本文整理汇总了Golang中github.com/GoogleCloudPlatform/kubernetes/pkg/util.NewIntOrStringFromString函数的典型用法代码示例。如果您正苦于以下问题:Golang NewIntOrStringFromString函数的具体用法?Golang NewIntOrStringFromString怎么用?Golang NewIntOrStringFromString使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了NewIntOrStringFromString函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: TestGetInteger
func TestGetInteger(t *testing.T) {
tests := []struct {
res api.ResourceList
name api.ResourceName
expected int
def int
test string
}{
{
res: api.ResourceList{},
name: CPU,
expected: 1,
def: 1,
test: "nothing present",
},
{
res: api.ResourceList{
CPU: util.NewIntOrStringFromInt(2),
},
name: CPU,
expected: 2,
def: 1,
test: "present",
},
{
res: api.ResourceList{
Memory: util.NewIntOrStringFromInt(2),
},
name: CPU,
expected: 1,
def: 1,
test: "not-present",
},
{
res: api.ResourceList{
CPU: util.NewIntOrStringFromString("2"),
},
name: CPU,
expected: 2,
def: 1,
test: "present-string",
},
{
res: api.ResourceList{
CPU: util.NewIntOrStringFromString("foo"),
},
name: CPU,
expected: 1,
def: 1,
test: "present-invalid",
},
}
for _, test := range tests {
val := GetIntegerResource(test.res, test.name, test.def)
if val != test.expected {
t.Errorf("expected: %d found %d", test.expected, val)
}
}
}
示例2: Instances
// Instances returns an implementation of Instances for OpenStack.
func (os *OpenStack) Instances() (cloudprovider.Instances, bool) {
servers, err := gophercloud.ServersApi(os.access, gophercloud.ApiCriteria{
Type: "compute",
UrlChoice: gophercloud.PublicURL,
Region: os.region,
})
if err != nil {
return nil, false
}
flavors, err := servers.ListFlavors()
if err != nil {
return nil, false
}
flavor_to_resource := make(map[string]*api.NodeResources, len(flavors))
for _, flavor := range flavors {
rsrc := api.NodeResources{
Capacity: api.ResourceList{
"cpu": util.NewIntOrStringFromInt(flavor.VCpus),
"memory": util.NewIntOrStringFromString(fmt.Sprintf("%dMiB", flavor.Ram)),
"openstack.org/disk": util.NewIntOrStringFromString(fmt.Sprintf("%dGB", flavor.Disk)),
"openstack.org/rxTxFactor": util.NewIntOrStringFromInt(int(flavor.RxTxFactor * 1000)),
"openstack.org/swap": util.NewIntOrStringFromString(fmt.Sprintf("%dMiB", flavor.Swap)),
},
}
flavor_to_resource[flavor.Id] = &rsrc
}
return &Instances{servers, flavor_to_resource}, true
}
示例3: TestGetURLParts
func TestGetURLParts(t *testing.T) {
testCases := []struct {
probe *api.HTTPGetAction
ok bool
host string
port int
path string
}{
{&api.HTTPGetAction{Host: "", Port: util.NewIntOrStringFromInt(-1), Path: ""}, false, "", -1, ""},
{&api.HTTPGetAction{Host: "", Port: util.NewIntOrStringFromString(""), Path: ""}, false, "", -1, ""},
{&api.HTTPGetAction{Host: "", Port: util.NewIntOrStringFromString("-1"), Path: ""}, false, "", -1, ""},
{&api.HTTPGetAction{Host: "", Port: util.NewIntOrStringFromString("not-found"), Path: ""}, false, "", -1, ""},
{&api.HTTPGetAction{Host: "", Port: util.NewIntOrStringFromString("found"), Path: ""}, true, "127.0.0.1", 93, ""},
{&api.HTTPGetAction{Host: "", Port: util.NewIntOrStringFromInt(76), Path: ""}, true, "127.0.0.1", 76, ""},
{&api.HTTPGetAction{Host: "", Port: util.NewIntOrStringFromString("118"), Path: ""}, true, "127.0.0.1", 118, ""},
{&api.HTTPGetAction{Host: "hostname", Port: util.NewIntOrStringFromInt(76), Path: "path"}, true, "hostname", 76, "path"},
}
for _, test := range testCases {
state := api.PodStatus{PodIP: "127.0.0.1"}
container := api.Container{
Ports: []api.ContainerPort{{Name: "found", HostPort: 93}},
LivenessProbe: &api.Probe{
Handler: api.Handler{
HTTPGet: test.probe,
},
},
}
scheme := test.probe.Scheme
if scheme == "" {
scheme = api.URISchemeHTTP
}
host := test.probe.Host
if host == "" {
host = state.PodIP
}
port, err := extractPort(test.probe.Port, container)
if test.ok && err != nil {
t.Errorf("Unexpected error: %v", err)
}
path := test.probe.Path
if !test.ok && err == nil {
t.Errorf("Expected error for %+v, got %s%s:%d/%s", test, scheme, host, port, path)
}
if test.ok {
if host != test.host || port != test.port || path != test.path {
t.Errorf("Expected %s:%d/%s, got %s:%d/%s",
test.host, test.port, test.path, host, port, path)
}
}
}
}
示例4: TestGetFloat
func TestGetFloat(t *testing.T) {
tests := []struct {
res api.ResourceList
name api.ResourceName
expected float64
def float64
test string
}{
{
res: api.ResourceList{},
name: CPU,
expected: 1.5,
def: 1.5,
test: "nothing present",
},
{
res: api.ResourceList{
CPU: util.NewIntOrStringFromInt(2),
},
name: CPU,
expected: 2.0,
def: 1.5,
test: "present",
},
{
res: api.ResourceList{
CPU: util.NewIntOrStringFromString("2.5"),
},
name: CPU,
expected: 2.5,
def: 1,
test: "present-string",
},
{
res: api.ResourceList{
CPU: util.NewIntOrStringFromString("foo"),
},
name: CPU,
expected: 1,
def: 1,
test: "present-invalid",
},
}
for _, test := range tests {
val := GetFloatResource(test.res, test.name, test.def)
if val != test.expected {
t.Errorf("expected: %d found %d", test.expected, val)
}
}
}
示例5: TestGetString
func TestGetString(t *testing.T) {
tests := []struct {
res api.ResourceList
name api.ResourceName
expected string
def string
test string
}{
{
res: api.ResourceList{},
name: CPU,
expected: "foo",
def: "foo",
test: "nothing present",
},
{
res: api.ResourceList{
CPU: util.NewIntOrStringFromString("bar"),
},
name: CPU,
expected: "bar",
def: "foo",
test: "present",
},
}
for _, test := range tests {
val := GetStringResource(test.res, test.name, test.def)
if val != test.expected {
t.Errorf("expected: %d found %d", test.expected, val)
}
}
}
示例6: TestSetDefaultServicePort
func TestSetDefaultServicePort(t *testing.T) {
// Unchanged if set.
in := &versioned.Service{Spec: versioned.ServiceSpec{
Ports: []versioned.ServicePort{
{Protocol: "UDP", Port: 9376, TargetPort: util.NewIntOrStringFromString("p")},
{Protocol: "UDP", Port: 8675, TargetPort: util.NewIntOrStringFromInt(309)},
},
}}
out := roundTrip(t, runtime.Object(in)).(*versioned.Service)
if out.Spec.Ports[0].Protocol != versioned.ProtocolUDP {
t.Errorf("Expected protocol %s, got %s", versioned.ProtocolUDP, out.Spec.Ports[0].Protocol)
}
if out.Spec.Ports[0].TargetPort != util.NewIntOrStringFromString("p") {
t.Errorf("Expected port %d, got %s", in.Spec.Ports[0].Port, out.Spec.Ports[0].TargetPort)
}
if out.Spec.Ports[1].Protocol != versioned.ProtocolUDP {
t.Errorf("Expected protocol %s, got %s", versioned.ProtocolUDP, out.Spec.Ports[1].Protocol)
}
if out.Spec.Ports[1].TargetPort != util.NewIntOrStringFromInt(309) {
t.Errorf("Expected port %d, got %s", in.Spec.Ports[1].Port, out.Spec.Ports[1].TargetPort)
}
// Defaulted.
in = &versioned.Service{Spec: versioned.ServiceSpec{
Ports: []versioned.ServicePort{
{Protocol: "", Port: 9376, TargetPort: util.NewIntOrStringFromString("")},
{Protocol: "", Port: 8675, TargetPort: util.NewIntOrStringFromInt(0)},
},
}}
out = roundTrip(t, runtime.Object(in)).(*versioned.Service)
if out.Spec.Ports[0].Protocol != versioned.ProtocolTCP {
t.Errorf("Expected protocol %s, got %s", versioned.ProtocolTCP, out.Spec.Ports[0].Protocol)
}
if out.Spec.Ports[0].TargetPort != util.NewIntOrStringFromInt(in.Spec.Ports[0].Port) {
t.Errorf("Expected port %d, got %d", in.Spec.Ports[0].Port, out.Spec.Ports[0].TargetPort)
}
if out.Spec.Ports[1].Protocol != versioned.ProtocolTCP {
t.Errorf("Expected protocol %s, got %s", versioned.ProtocolTCP, out.Spec.Ports[1].Protocol)
}
if out.Spec.Ports[1].TargetPort != util.NewIntOrStringFromInt(in.Spec.Ports[1].Port) {
t.Errorf("Expected port %d, got %d", in.Spec.Ports[1].Port, out.Spec.Ports[1].TargetPort)
}
}
示例7: TestGetURLParts
func TestGetURLParts(t *testing.T) {
testCases := []struct {
probe *api.HTTPGetProbe
ok bool
host string
port int
path string
}{
{&api.HTTPGetProbe{Host: "", Port: util.NewIntOrStringFromInt(-1), Path: ""}, false, "", -1, ""},
{&api.HTTPGetProbe{Host: "", Port: util.NewIntOrStringFromString(""), Path: ""}, false, "", -1, ""},
{&api.HTTPGetProbe{Host: "", Port: util.NewIntOrStringFromString("-1"), Path: ""}, false, "", -1, ""},
{&api.HTTPGetProbe{Host: "", Port: util.NewIntOrStringFromString("not-found"), Path: ""}, false, "", -1, ""},
{&api.HTTPGetProbe{Host: "", Port: util.NewIntOrStringFromString("found"), Path: ""}, true, "127.0.0.1", 93, ""},
{&api.HTTPGetProbe{Host: "", Port: util.NewIntOrStringFromInt(76), Path: ""}, true, "127.0.0.1", 76, ""},
{&api.HTTPGetProbe{Host: "", Port: util.NewIntOrStringFromString("118"), Path: ""}, true, "127.0.0.1", 118, ""},
{&api.HTTPGetProbe{Host: "hostname", Port: util.NewIntOrStringFromInt(76), Path: "path"}, true, "hostname", 76, "path"},
}
for _, test := range testCases {
state := api.PodState{PodIP: "127.0.0.1"}
container := api.Container{
Ports: []api.Port{{Name: "found", HostPort: 93}},
LivenessProbe: &api.LivenessProbe{
HTTPGet: test.probe,
Type: "http",
},
}
host, port, path, err := getURLParts(state, container)
if !test.ok && err == nil {
t.Errorf("Expected error for %+v, got %s:%d/%s", test, host, port, path)
}
if test.ok && err != nil {
t.Errorf("Unexpected error: %v", err)
}
if test.ok {
if host != test.host || port != test.port || path != test.path {
t.Errorf("Expected %s:%d/%s, got %s:%d/%s",
test.host, test.port, test.path, host, port, path)
}
}
}
}
示例8: TestGetTCPAddrParts
func TestGetTCPAddrParts(t *testing.T) {
testCases := []struct {
probe *api.TCPSocketAction
ok bool
host string
port int
}{
{&api.TCPSocketAction{Port: util.NewIntOrStringFromInt(-1)}, false, "", -1},
{&api.TCPSocketAction{Port: util.NewIntOrStringFromString("")}, false, "", -1},
{&api.TCPSocketAction{Port: util.NewIntOrStringFromString("-1")}, false, "", -1},
{&api.TCPSocketAction{Port: util.NewIntOrStringFromString("not-found")}, false, "", -1},
{&api.TCPSocketAction{Port: util.NewIntOrStringFromString("found")}, true, "1.2.3.4", 93},
{&api.TCPSocketAction{Port: util.NewIntOrStringFromInt(76)}, true, "1.2.3.4", 76},
{&api.TCPSocketAction{Port: util.NewIntOrStringFromString("118")}, true, "1.2.3.4", 118},
}
for _, test := range testCases {
host := "1.2.3.4"
container := api.Container{
Ports: []api.ContainerPort{{Name: "found", HostPort: 93}},
LivenessProbe: &api.Probe{
Handler: api.Handler{
TCPSocket: test.probe,
},
},
}
port, err := extractPort(test.probe.Port, container)
if !test.ok && err == nil {
t.Errorf("Expected error for %+v, got %s:%d", test, host, port)
}
if test.ok && err != nil {
t.Errorf("Unexpected error: %v", err)
}
if test.ok {
if host != test.host || port != test.port {
t.Errorf("Expected %s:%d, got %s:%d", test.host, test.port, host, port)
}
}
}
}
示例9: TestHTTPHealthChecker
func TestHTTPHealthChecker(t *testing.T) {
testCases := []struct {
probe *api.HTTPGetAction
status int
health Status
}{
// The probe will be filled in below. This is primarily testing that an HTTP GET happens.
{&api.HTTPGetAction{}, http.StatusOK, Healthy},
{&api.HTTPGetAction{}, -1, Unhealthy},
{nil, -1, Unknown},
}
hc := &HTTPHealthChecker{
client: &http.Client{},
}
for _, test := range testCases {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(test.status)
}))
u, err := url.Parse(ts.URL)
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
host, port, err := net.SplitHostPort(u.Host)
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
container := api.Container{
LivenessProbe: &api.LivenessProbe{
HTTPGet: test.probe,
Type: "http",
},
}
params := container.LivenessProbe.HTTPGet
if params != nil {
params.Port = util.NewIntOrStringFromString(port)
params.Host = host
}
health, err := hc.HealthCheck("test", api.PodState{PodIP: host}, container)
if test.health == Unknown && err == nil {
t.Errorf("Expected error")
}
if test.health != Unknown && err != nil {
t.Errorf("Unexpected error: %v", err)
}
if health != test.health {
t.Errorf("Expected %v, got %v", test.health, health)
}
}
}
示例10: TestHealthChecker
func TestHealthChecker(t *testing.T) {
AddHealthChecker(&HTTPHealthChecker{client: &http.Client{}})
var healthCheckerTests = []struct {
status int
health Status
}{
{http.StatusOK, Healthy},
{statusServerEarlyShutdown, Unknown},
{http.StatusBadRequest, Unhealthy},
{http.StatusBadGateway, Unhealthy},
{http.StatusInternalServerError, Unhealthy},
}
for _, healthCheckerTest := range healthCheckerTests {
tt := healthCheckerTest
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(tt.status)
}))
defer ts.Close()
u, err := url.Parse(ts.URL)
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
host, port, err := net.SplitHostPort(u.Host)
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
if tt.status == statusServerEarlyShutdown {
ts.Close()
}
container := api.Container{
LivenessProbe: &api.LivenessProbe{
HTTPGet: &api.HTTPGetAction{
Port: util.NewIntOrStringFromString(port),
Path: "/foo/bar",
Host: host,
},
},
}
hc := NewHealthChecker()
health, err := hc.HealthCheck("test", "", api.PodState{}, container)
if err != nil && tt.health != Unknown {
t.Errorf("Unexpected error: %v", err)
}
if health != tt.health {
t.Errorf("Expected %v, got %v", tt.health, health)
}
}
}
示例11: TestTcpHealthChecker
func TestTcpHealthChecker(t *testing.T) {
tests := []struct {
probe *api.TCPSocketProbe
expectedStatus Status
expectError bool
}{
// The probe will be filled in below. This is primarily testing that a connection is made.
{&api.TCPSocketProbe{}, Healthy, false},
{&api.TCPSocketProbe{}, Unhealthy, false},
{nil, Unknown, true},
}
checker := &TCPHealthChecker{}
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
}))
u, err := url.Parse(server.URL)
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
host, port, err := net.SplitHostPort(u.Host)
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
for _, test := range tests {
container := api.Container{
LivenessProbe: &api.LivenessProbe{
TCPSocket: test.probe,
Type: "tcp",
},
}
params := container.LivenessProbe.TCPSocket
if params != nil && test.expectedStatus == Healthy {
params.Port = util.NewIntOrStringFromString(port)
}
status, err := checker.HealthCheck("test", api.PodState{PodIP: host}, container)
if status != test.expectedStatus {
t.Errorf("expected: %v, got: %v", test.expectedStatus, status)
}
if err != nil && !test.expectError {
t.Errorf("unexpected error: %#v", err)
}
if err == nil && test.expectError {
t.Errorf("unexpected non-error.")
}
}
}
示例12: Generate
func (ServiceGenerator) Generate(params map[string]string) (runtime.Object, error) {
selectorString, found := params["selector"]
if !found || len(selectorString) == 0 {
return nil, fmt.Errorf("'selector' is a required parameter.")
}
selector := ParseLabels(selectorString)
name, found := params["name"]
if !found {
return nil, fmt.Errorf("'name' is a required parameter.")
}
portString, found := params["port"]
if !found {
return nil, fmt.Errorf("'port' is a required parameter.")
}
port, err := strconv.Atoi(portString)
if err != nil {
return nil, err
}
service := api.Service{
ObjectMeta: api.ObjectMeta{
Name: name,
},
Spec: api.ServiceSpec{
Port: port,
Protocol: api.Protocol(params["protocol"]),
Selector: selector,
},
}
containerPort, found := params["container-port"]
if found && len(containerPort) > 0 {
if cPort, err := strconv.Atoi(containerPort); err != nil {
service.Spec.ContainerPort = util.NewIntOrStringFromString(containerPort)
} else {
service.Spec.ContainerPort = util.NewIntOrStringFromInt(cPort)
}
} else {
service.Spec.ContainerPort = util.NewIntOrStringFromInt(port)
}
if params["create-external-load-balancer"] == "true" {
service.Spec.CreateExternalLoadBalancer = true
}
if len(params["public-ip"]) != 0 {
service.Spec.PublicIPs = []string{params["public-ip"]}
}
return &service, nil
}
示例13: TestMuxHealthChecker
func TestMuxHealthChecker(t *testing.T) {
muxHealthCheckerTests := []struct {
health Status
probeType string
}{
{Healthy, "http"},
{Unknown, "ftp"},
}
mc := &muxHealthChecker{
checkers: make(map[string]HealthChecker),
}
hc := &HTTPHealthChecker{
client: &http.Client{},
}
mc.checkers["http"] = hc
for _, muxHealthCheckerTest := range muxHealthCheckerTests {
tt := muxHealthCheckerTest
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
}))
u, err := url.Parse(ts.URL)
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
host, port, err := net.SplitHostPort(u.Host)
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
container := api.Container{
LivenessProbe: &api.LivenessProbe{
HTTPGet: &api.HTTPGetProbe{},
},
}
container.LivenessProbe.Type = tt.probeType
container.LivenessProbe.HTTPGet.Port = util.NewIntOrStringFromString(port)
container.LivenessProbe.HTTPGet.Host = host
health, err := mc.HealthCheck("test", api.PodState{}, container)
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
if health != tt.health {
t.Errorf("Expected %v, got %v", tt.health, health)
}
}
}
示例14: TestMuxHealthChecker
func TestMuxHealthChecker(t *testing.T) {
muxHealthCheckerTests := []struct {
health Status
}{
// TODO: This test should run through a few different checker types.
{Healthy},
}
mc := &muxHealthChecker{
checkers: []HealthChecker{
&HTTPHealthChecker{client: &http.Client{}},
},
}
for _, muxHealthCheckerTest := range muxHealthCheckerTests {
tt := muxHealthCheckerTest
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
}))
defer ts.Close()
u, err := url.Parse(ts.URL)
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
host, port, err := net.SplitHostPort(u.Host)
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
container := api.Container{
LivenessProbe: &api.LivenessProbe{
HTTPGet: &api.HTTPGetAction{},
},
}
container.LivenessProbe.HTTPGet.Port = util.NewIntOrStringFromString(port)
container.LivenessProbe.HTTPGet.Host = host
health, err := mc.HealthCheck("test", "", api.PodState{}, container)
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
if health != tt.health {
t.Errorf("Expected %v, got %v", tt.health, health)
}
}
}
示例15: TestGetEndpoints
func TestGetEndpoints(t *testing.T) {
// 2 pods each of which have 3 targetPorts exposed via a single service
endpointAddresses := []api.EndpointAddress{
{IP: "1.2.3.4"},
{IP: "6.7.8.9"},
}
ports := []int{80, 443, 3306}
endpointPorts := []api.EndpointPort{
{Port: ports[0], Protocol: "TCP"},
{Port: ports[1], Protocol: "TCP"},
{Port: ports[2], Protocol: "TCP", Name: "mysql"},
}
servicePorts := []api.ServicePort{
{Port: 10, TargetPort: util.NewIntOrStringFromInt(ports[0])},
{Port: 20, TargetPort: util.NewIntOrStringFromInt(ports[1])},
{Port: 30, TargetPort: util.NewIntOrStringFromString("mysql")},
}
svc := getService(servicePorts)
endpoints := []*api.Endpoints{getEndpoints(svc, endpointAddresses, endpointPorts)}
flb := newFakeLoadBalancerController(endpoints, []*api.Service{svc})
for i := range ports {
eps := flb.getEndpoints(svc, &svc.Spec.Ports[i])
expectedEps := util.NewStringSet()
for _, address := range endpointAddresses {
expectedEps.Insert(fmt.Sprintf("%v:%v", address.IP, ports[i]))
}
receivedEps := util.NewStringSet()
for _, ep := range eps {
receivedEps.Insert(ep)
}
if len(receivedEps) != len(expectedEps) || !expectedEps.IsSuperset(receivedEps) {
t.Fatalf("Unexpected endpoints, received %+v, expected %+v", receivedEps, expectedEps)
}
glog.Infof("Got endpoints %+v", receivedEps)
}
}