本文整理匯總了Golang中k8s/io/kubernetes/pkg/api.NewDefaultContext函數的典型用法代碼示例。如果您正苦於以下問題:Golang NewDefaultContext函數的具體用法?Golang NewDefaultContext怎麽用?Golang NewDefaultContext使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了NewDefaultContext函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: TestRESTUpdate
func TestRESTUpdate(t *testing.T) {
_, rest := NewTestREST()
eventA := testEvent("foo")
_, err := rest.Create(api.NewDefaultContext(), eventA)
if err != nil {
t.Fatalf("Unexpected error %v", err)
}
got, err := rest.Get(api.NewDefaultContext(), eventA.Name)
if err != nil {
t.Fatalf("Unexpected error %v", err)
}
if e, a := eventA, got; !reflect.DeepEqual(e, a) {
t.Errorf("diff: %s", util.ObjectDiff(e, a))
}
eventB := testEvent("bar")
_, _, err = rest.Update(api.NewDefaultContext(), eventB)
if err != nil {
t.Fatalf("Unexpected error %v", err)
}
got2, err := rest.Get(api.NewDefaultContext(), eventB.Name)
if err != nil {
t.Fatalf("Unexpected error %v", err)
}
if e, a := eventB, got2; !reflect.DeepEqual(e, a) {
t.Errorf("diff: %s", util.ObjectDiff(e, a))
}
}
示例2: TestDeletePod
func TestDeletePod(t *testing.T) {
storage, _, _, fakeClient := newStorage(t)
fakeClient.ChangeIndex = 1
ctx := api.NewDefaultContext()
key, _ := storage.Etcd.KeyFunc(ctx, "foo")
key = etcdtest.AddPrefix(key)
fakeClient.Data[key] = tools.EtcdResponseWithError{
R: &etcd.Response{
Node: &etcd.Node{
Value: runtime.EncodeOrDie(testapi.Codec(), &api.Pod{
ObjectMeta: api.ObjectMeta{
Name: "foo",
Namespace: api.NamespaceDefault,
},
Spec: api.PodSpec{NodeName: "machine"},
}),
ModifiedIndex: 1,
CreatedIndex: 1,
},
},
}
_, err := storage.Delete(api.NewDefaultContext(), "foo", nil)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
}
示例3: TestGetNodeAddresses
// TestGetNodeAddresses verifies that proper results are returned
// when requesting node addresses.
func TestGetNodeAddresses(t *testing.T) {
master, etcdserver, _, assert := setUp(t)
defer etcdserver.Terminate(t)
// Fail case (no addresses associated with nodes)
nodes, _ := master.nodeRegistry.ListNodes(api.NewDefaultContext(), nil)
addrs, err := master.getNodeAddresses()
assert.Error(err, "getNodeAddresses should have caused an error as there are no addresses.")
assert.Equal([]string(nil), addrs)
// Pass case with External type IP
nodes, _ = master.nodeRegistry.ListNodes(api.NewDefaultContext(), nil)
for index := range nodes.Items {
nodes.Items[index].Status.Addresses = []api.NodeAddress{{Type: api.NodeExternalIP, Address: "127.0.0.1"}}
}
addrs, err = master.getNodeAddresses()
assert.NoError(err, "getNodeAddresses should not have returned an error.")
assert.Equal([]string{"127.0.0.1", "127.0.0.1"}, addrs)
// Pass case with LegacyHost type IP
nodes, _ = master.nodeRegistry.ListNodes(api.NewDefaultContext(), nil)
for index := range nodes.Items {
nodes.Items[index].Status.Addresses = []api.NodeAddress{{Type: api.NodeLegacyHostIP, Address: "127.0.0.2"}}
}
addrs, err = master.getNodeAddresses()
assert.NoError(err, "getNodeAddresses failback should not have returned an error.")
assert.Equal([]string{"127.0.0.2", "127.0.0.2"}, addrs)
}
示例4: TestInstantiateWithLastVersion
func TestInstantiateWithLastVersion(t *testing.T) {
g := mockBuildGenerator()
c := g.Client.(Client)
c.GetBuildConfigFunc = func(ctx kapi.Context, name string) (*buildapi.BuildConfig, error) {
bc := mocks.MockBuildConfig(mocks.MockSource(), mocks.MockSourceStrategyForImageRepository(), mocks.MockOutput())
bc.Status.LastVersion = 1
return bc, nil
}
g.Client = c
// Version not specified
_, err := g.Instantiate(kapi.NewDefaultContext(), &buildapi.BuildRequest{})
if err != nil {
t.Errorf("Unexpected error %v", err)
}
// Version specified and it matches
lastVersion := 1
_, err = g.Instantiate(kapi.NewDefaultContext(), &buildapi.BuildRequest{LastVersion: &lastVersion})
if err != nil {
t.Errorf("Unexpected error %v", err)
}
// Version specified, but doesn't match
lastVersion = 0
_, err = g.Instantiate(kapi.NewDefaultContext(), &buildapi.BuildRequest{LastVersion: &lastVersion})
if err == nil {
t.Errorf("Expected an error and did not get one")
}
}
示例5: TestInstantiateDeletingError
func TestInstantiateDeletingError(t *testing.T) {
generator := BuildGenerator{Client: Client{
GetBuildConfigFunc: func(ctx kapi.Context, name string) (*buildapi.BuildConfig, error) {
bc := &buildapi.BuildConfig{
ObjectMeta: kapi.ObjectMeta{
Annotations: map[string]string{
buildapi.BuildConfigPausedAnnotation: "true",
},
},
}
return bc, nil
},
GetBuildFunc: func(ctx kapi.Context, name string) (*buildapi.Build, error) {
build := &buildapi.Build{
Status: buildapi.BuildStatus{
Config: &kapi.ObjectReference{
Name: "buildconfig",
},
},
}
return build, nil
},
}}
_, err := generator.Instantiate(kapi.NewDefaultContext(), &buildapi.BuildRequest{})
if err == nil || !strings.Contains(err.Error(), "BuildConfig is paused") {
t.Errorf("Expected error, got different %v", err)
}
_, err = generator.Clone(kapi.NewDefaultContext(), &buildapi.BuildRequest{})
if err == nil || !strings.Contains(err.Error(), "BuildConfig is paused") {
t.Errorf("Expected error, got different %v", err)
}
}
示例6: TestUpdateWithConflictingNamespace
func TestUpdateWithConflictingNamespace(t *testing.T) {
storage, _, _, fakeClient := newStorage(t)
ctx := api.NewDefaultContext()
key, _ := storage.Etcd.KeyFunc(ctx, "foo")
key = etcdtest.AddPrefix(key)
fakeClient.Data[key] = tools.EtcdResponseWithError{
R: &etcd.Response{
Node: &etcd.Node{
Value: runtime.EncodeOrDie(testapi.Codec(), &api.Pod{
ObjectMeta: api.ObjectMeta{Name: "foo", Namespace: "default"},
Spec: api.PodSpec{NodeName: "machine"},
}),
ModifiedIndex: 1,
},
},
}
pod := validChangedPod()
pod.Namespace = "not-default"
obj, created, err := storage.Update(api.NewDefaultContext(), pod)
if obj != nil || created {
t.Error("Expected a nil channel, but we got a value or created")
}
if err == nil {
t.Errorf("Expected an error, but we didn't get one")
} else if strings.Index(err.Error(), "the namespace of the provided object does not match the namespace sent on the request") == -1 {
t.Errorf("Expected 'Pod.Namespace does not match the provided context' error, got '%v'", err.Error())
}
}
示例7: TestCreateImageStreamOK
func TestCreateImageStreamOK(t *testing.T) {
_, helper := newHelper(t)
storage, _ := NewREST(helper, noDefaultRegistry, &fakeSubjectAccessReviewRegistry{})
stream := &api.ImageStream{ObjectMeta: kapi.ObjectMeta{Name: "foo"}}
ctx := kapi.WithUser(kapi.NewDefaultContext(), &fakeUser{})
_, err := storage.Create(ctx, stream)
if err != nil {
t.Fatalf("Unexpected non-nil error: %#v", err)
}
actual := &api.ImageStream{}
if err := helper.Get(kapi.NewDefaultContext(), "/imagestreams/default/foo", actual, false); err != nil {
t.Fatalf("unexpected extraction error: %v", err)
}
if actual.Name != stream.Name {
t.Errorf("unexpected stream: %#v", actual)
}
if len(actual.UID) == 0 {
t.Errorf("expected stream UID to be set: %#v", actual)
}
if stream.CreationTimestamp.IsZero() {
t.Error("Unexpected zero CreationTimestamp")
}
if stream.Spec.DockerImageRepository != "" {
t.Errorf("unexpected stream: %#v", stream)
}
}
示例8: TestDeleteResourceQuota
func TestDeleteResourceQuota(t *testing.T) {
fakeEtcdClient, etcdStorage := newEtcdStorage(t)
fakeEtcdClient.ChangeIndex = 1
storage, _ := NewStorage(etcdStorage)
ctx := api.NewDefaultContext()
key, _ := storage.Etcd.KeyFunc(ctx, "foo")
key = etcdtest.AddPrefix(key)
fakeEtcdClient.Data[key] = tools.EtcdResponseWithError{
R: &etcd.Response{
Node: &etcd.Node{
Value: runtime.EncodeOrDie(latest.Codec, &api.ResourceQuota{
ObjectMeta: api.ObjectMeta{
Name: "foo",
Namespace: api.NamespaceDefault,
},
Status: api.ResourceQuotaStatus{},
}),
ModifiedIndex: 1,
CreatedIndex: 1,
},
},
}
_, err := storage.Delete(api.NewDefaultContext(), "foo", nil)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
}
示例9: TestNoOpUpdates
func TestNoOpUpdates(t *testing.T) {
server, registry := NewTestGenericStoreRegistry(t)
defer server.Terminate(t)
newPod := func() *api.Pod {
return &api.Pod{
ObjectMeta: api.ObjectMeta{
Namespace: api.NamespaceDefault,
Name: "foo",
Labels: map[string]string{"prepare_create": "true"},
},
Spec: api.PodSpec{NodeName: "machine"},
}
}
var err error
var createResult runtime.Object
if createResult, err = registry.Create(api.NewDefaultContext(), newPod()); err != nil {
t.Fatalf("Unexpected error: %v", err)
}
createdPod, err := registry.Get(api.NewDefaultContext(), "foo")
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
var updateResult runtime.Object
p := newPod()
if updateResult, _, err = registry.Update(api.NewDefaultContext(), p.Name, rest.DefaultUpdatedObjectInfo(p, api.Scheme)); err != nil {
t.Fatalf("Unexpected error: %v", err)
}
// Check whether we do not return empty result on no-op update.
if !reflect.DeepEqual(createResult, updateResult) {
t.Errorf("no-op update should return a correct value, got: %#v", updateResult)
}
updatedPod, err := registry.Get(api.NewDefaultContext(), "foo")
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
createdMeta, err := meta.Accessor(createdPod)
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
updatedMeta, err := meta.Accessor(updatedPod)
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
if createdMeta.GetResourceVersion() != updatedMeta.GetResourceVersion() {
t.Errorf("no-op update should be ignored and not written to etcd")
}
}
示例10: TestServiceRegistryIPReallocation
func TestServiceRegistryIPReallocation(t *testing.T) {
storage, _ := NewTestREST(t, nil)
svc1 := &api.Service{
ObjectMeta: api.ObjectMeta{Name: "foo"},
Spec: api.ServiceSpec{
Selector: map[string]string{"bar": "baz"},
SessionAffinity: api.ServiceAffinityNone,
Type: api.ServiceTypeClusterIP,
Ports: []api.ServicePort{{
Port: 6502,
Protocol: api.ProtocolTCP,
TargetPort: intstr.FromInt(6502),
}},
},
}
ctx := api.NewDefaultContext()
created_svc1, _ := storage.Create(ctx, svc1)
created_service_1 := created_svc1.(*api.Service)
if created_service_1.Name != "foo" {
t.Errorf("Expected foo, but got %v", created_service_1.Name)
}
if !makeIPNet(t).Contains(net.ParseIP(created_service_1.Spec.ClusterIP)) {
t.Errorf("Unexpected ClusterIP: %s", created_service_1.Spec.ClusterIP)
}
_, err := storage.Delete(ctx, created_service_1.Name)
if err != nil {
t.Errorf("Unexpected error deleting service: %v", err)
}
svc2 := &api.Service{
ObjectMeta: api.ObjectMeta{Name: "bar"},
Spec: api.ServiceSpec{
Selector: map[string]string{"bar": "baz"},
SessionAffinity: api.ServiceAffinityNone,
Type: api.ServiceTypeClusterIP,
Ports: []api.ServicePort{{
Port: 6502,
Protocol: api.ProtocolTCP,
TargetPort: intstr.FromInt(6502),
}},
},
}
ctx = api.NewDefaultContext()
created_svc2, _ := storage.Create(ctx, svc2)
created_service_2 := created_svc2.(*api.Service)
if created_service_2.Name != "bar" {
t.Errorf("Expected bar, but got %v", created_service_2.Name)
}
if !makeIPNet(t).Contains(net.ParseIP(created_service_2.Spec.ClusterIP)) {
t.Errorf("Unexpected ClusterIP: %s", created_service_2.Spec.ClusterIP)
}
}
示例11: TestRESTDelete
func TestRESTDelete(t *testing.T) {
_, rest := NewTestREST()
eventA := testEvent("foo")
_, err := rest.Create(api.NewDefaultContext(), eventA)
if err != nil {
t.Fatalf("Unexpected error %v", err)
}
c, err := rest.Delete(api.NewDefaultContext(), eventA.Name)
if err != nil {
t.Fatalf("Unexpected error %v", err)
}
if stat := c.(*api.Status); stat.Status != api.StatusSuccess {
t.Errorf("unexpected status: %v", stat)
}
}
示例12: TestNamespaceStatusStrategy
func TestNamespaceStatusStrategy(t *testing.T) {
ctx := api.NewDefaultContext()
if StatusStrategy.NamespaceScoped() {
t.Errorf("Namespaces should not be namespace scoped")
}
if StatusStrategy.AllowCreateOnUpdate() {
t.Errorf("Namespaces should not allow create on update")
}
now := unversioned.Now()
oldNamespace := &api.Namespace{
ObjectMeta: api.ObjectMeta{Name: "foo", ResourceVersion: "10", DeletionTimestamp: &now},
Spec: api.NamespaceSpec{Finalizers: []api.FinalizerName{"kubernetes"}},
Status: api.NamespaceStatus{Phase: api.NamespaceActive},
}
namespace := &api.Namespace{
ObjectMeta: api.ObjectMeta{Name: "foo", ResourceVersion: "9", DeletionTimestamp: &now},
Status: api.NamespaceStatus{Phase: api.NamespaceTerminating},
}
StatusStrategy.PrepareForUpdate(ctx, namespace, oldNamespace)
if namespace.Status.Phase != api.NamespaceTerminating {
t.Errorf("Namespace status updates should allow change of phase: %v", namespace.Status.Phase)
}
if len(namespace.Spec.Finalizers) != 1 || namespace.Spec.Finalizers[0] != api.FinalizerKubernetes {
t.Errorf("PrepareForUpdate should have preserved old finalizers")
}
errs := StatusStrategy.ValidateUpdate(ctx, namespace, oldNamespace)
if len(errs) != 0 {
t.Errorf("Unexpected error %v", errs)
}
if namespace.ResourceVersion != "9" {
t.Errorf("Incoming resource version on update should not be mutated")
}
}
示例13: TestEtcdCreateWithExistingContainers
func TestEtcdCreateWithExistingContainers(t *testing.T) {
storage, bindingStorage, _, server := newStorage(t)
defer server.Terminate(t)
ctx := api.NewDefaultContext()
key, _ := storage.KeyFunc(ctx, "foo")
key = etcdtest.AddPrefix(key)
_, err := storage.Create(ctx, validNewPod())
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
// Suddenly, a wild scheduler appears:
_, err = bindingStorage.Create(ctx, &api.Binding{
ObjectMeta: api.ObjectMeta{Namespace: api.NamespaceDefault, Name: "foo"},
Target: api.ObjectReference{Name: "machine"},
})
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
_, err = storage.Get(ctx, "foo")
if err != nil {
t.Fatalf("Unexpected error %v", err)
}
}
示例14: TestEtcdCreateWithContainersNotFound
func TestEtcdCreateWithContainersNotFound(t *testing.T) {
storage, bindingStorage, _, server := newStorage(t)
defer server.Terminate(t)
ctx := api.NewDefaultContext()
key, _ := storage.KeyFunc(ctx, "foo")
key = etcdtest.AddPrefix(key)
_, err := storage.Create(ctx, validNewPod())
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
// Suddenly, a wild scheduler appears:
_, err = bindingStorage.Create(ctx, &api.Binding{
ObjectMeta: api.ObjectMeta{
Namespace: api.NamespaceDefault,
Name: "foo",
Annotations: map[string]string{"label1": "value1"},
},
Target: api.ObjectReference{Name: "machine"},
})
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
obj, err := storage.Get(ctx, "foo")
if err != nil {
t.Fatalf("Unexpected error %v", err)
}
pod := obj.(*api.Pod)
if !(pod.Annotations != nil && pod.Annotations["label1"] == "value1") {
t.Fatalf("Pod annotations don't match the expected: %v", pod.Annotations)
}
}
示例15: TestDelete
func TestDelete(t *testing.T) {
ctx := api.NewDefaultContext()
storage, fakeEtcdClient, _ := newStorage(t)
test := resttest.New(t, storage, fakeEtcdClient.SetError)
rsrc := validNewThirdPartyResource("foo2")
key, _ := storage.KeyFunc(ctx, "foo2")
key = etcdtest.AddPrefix(key)
createFn := func() runtime.Object {
fakeEtcdClient.Data[key] = tools.EtcdResponseWithError{
R: &etcd.Response{
Node: &etcd.Node{
Value: runtime.EncodeOrDie(testapi.Codec(), rsrc),
ModifiedIndex: 1,
},
},
}
return rsrc
}
gracefulSetFn := func() bool {
if fakeEtcdClient.Data[key].R.Node == nil {
return false
}
return fakeEtcdClient.Data[key].R.Node.TTL == 30
}
test.TestDeleteNoGraceful(createFn, gracefulSetFn)
}