|
| 1 | +/* |
| 2 | +Copyright 2018 The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package mocks |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "testing" |
| 22 | + |
| 23 | + v1 "k8s.io/api/core/v1" |
| 24 | + apierrors "k8s.io/apimachinery/pkg/api/errors" |
| 25 | + "k8s.io/apimachinery/pkg/runtime" |
| 26 | + "k8s.io/apimachinery/pkg/types" |
| 27 | + clientgoscheme "k8s.io/client-go/kubernetes/scheme" |
| 28 | +) |
| 29 | + |
| 30 | +func TestGetNotFound(t *testing.T) { |
| 31 | + scheme := runtime.NewScheme() |
| 32 | + if err := clientgoscheme.AddToScheme(scheme); err != nil { |
| 33 | + t.Fatalf("failed to build scheme: %v", err) |
| 34 | + } |
| 35 | + |
| 36 | + ctx := context.Background() |
| 37 | + client := NewClient(scheme) |
| 38 | + |
| 39 | + node := v1.Node{} |
| 40 | + err := client.Get(ctx, types.NamespacedName{Name: "foo"}, &node) |
| 41 | + if !apierrors.IsNotFound(err) { |
| 42 | + t.Fatalf("expected IsNotFound error from non-existent get, got %v", err) |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +func TestCreateAndGet(t *testing.T) { |
| 47 | + scheme := runtime.NewScheme() |
| 48 | + if err := clientgoscheme.AddToScheme(scheme); err != nil { |
| 49 | + t.Fatalf("failed to build scheme: %v", err) |
| 50 | + } |
| 51 | + |
| 52 | + ctx := context.Background() |
| 53 | + client := NewClient(scheme) |
| 54 | + |
| 55 | + node := v1.Node{} |
| 56 | + |
| 57 | + err := client.Get(ctx, types.NamespacedName{Name: "foo"}, &node) |
| 58 | + if !apierrors.IsNotFound(err) { |
| 59 | + t.Fatalf("expected IsNotFound error from non-existent get, got %v", err) |
| 60 | + } |
| 61 | + |
| 62 | + node.Name = "foo" |
| 63 | + node.Spec.PodCIDR = "10.0.0.0/8" |
| 64 | + |
| 65 | + err = client.Create(ctx, &node) |
| 66 | + if err != nil { |
| 67 | + t.Fatalf("expected Create(node) to succeed; got error %v", err) |
| 68 | + } |
| 69 | + |
| 70 | + fetched := v1.Node{} |
| 71 | + err = client.Get(ctx, types.NamespacedName{Name: "foo"}, &fetched) |
| 72 | + if err != nil { |
| 73 | + t.Fatalf("expected Get(node) to succeed; got error %v", err) |
| 74 | + } |
| 75 | + |
| 76 | + if fetched.Spec.PodCIDR != "10.0.0.0/8" { |
| 77 | + t.Fatalf("expected node to round-trip, but PodCIDR was %q", fetched.Spec.PodCIDR) |
| 78 | + } |
| 79 | +} |
0 commit comments