|
| 1 | +package manifestclienttest |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "net/http" |
| 6 | + "testing" |
| 7 | + "time" |
| 8 | + |
| 9 | + configinformers "github.com/openshift/client-go/config/informers/externalversions" |
| 10 | + "k8s.io/client-go/tools/cache" |
| 11 | + |
| 12 | + "github.com/davecgh/go-spew/spew" |
| 13 | + configclient "github.com/openshift/client-go/config/clientset/versioned" |
| 14 | + apierrors "k8s.io/apimachinery/pkg/api/errors" |
| 15 | + "k8s.io/client-go/rest" |
| 16 | +) |
| 17 | + |
| 18 | +func TestBasicInformer(t *testing.T) { |
| 19 | + tests := []struct { |
| 20 | + name string |
| 21 | + testFn func(*testing.T, *http.Client) |
| 22 | + }{ |
| 23 | + { |
| 24 | + name: "informer-synced-compiled-client-with-data", |
| 25 | + testFn: func(t *testing.T, httpClient *http.Client) { |
| 26 | + ctx, cancel := context.WithCancel(context.Background()) |
| 27 | + defer cancel() |
| 28 | + |
| 29 | + configClient, err := configclient.NewForConfigAndClient(&rest.Config{}, httpClient) |
| 30 | + if err != nil { |
| 31 | + t.Fatal(err) |
| 32 | + } |
| 33 | + informers := configinformers.NewSharedInformerFactory(configClient, 0) |
| 34 | + featureGateInformer := informers.Config().V1().FeatureGates() |
| 35 | + featureGateInformer.Informer() |
| 36 | + informers.Start(ctx.Done()) |
| 37 | + |
| 38 | + timedOutCh := make(chan struct{}) |
| 39 | + go func() { |
| 40 | + select { |
| 41 | + case <-time.After(1 * time.Second): |
| 42 | + close(timedOutCh) |
| 43 | + |
| 44 | + } |
| 45 | + }() |
| 46 | + if !cache.WaitForCacheSync(timedOutCh, featureGateInformer.Informer().HasSynced) { |
| 47 | + t.Fatal("failed to sync") |
| 48 | + } |
| 49 | + |
| 50 | + featureGatesCluster, err := featureGateInformer.Lister().Get("cluster") |
| 51 | + if err != nil { |
| 52 | + t.Fatal(err) |
| 53 | + } |
| 54 | + if len(featureGatesCluster.Status.FeatureGates) == 0 { |
| 55 | + t.Fatal(spew.Sdump(featureGatesCluster)) |
| 56 | + } |
| 57 | + missing, err := featureGateInformer.Lister().Get("missing") |
| 58 | + if !apierrors.IsNotFound(err) { |
| 59 | + t.Fatal(err) |
| 60 | + } |
| 61 | + if missing != nil { |
| 62 | + t.Fatal(missing) |
| 63 | + } |
| 64 | + }, |
| 65 | + }, |
| 66 | + } |
| 67 | + |
| 68 | + for _, roundTripperTest := range defaultRoundTrippers(t) { |
| 69 | + t.Run(roundTripperTest.name, func(t *testing.T) { |
| 70 | + for _, test := range tests { |
| 71 | + t.Run(test.name, func(t *testing.T) { |
| 72 | + test.testFn(t, roundTripperTest.getClient()) |
| 73 | + }) |
| 74 | + } |
| 75 | + }) |
| 76 | + } |
| 77 | +} |
0 commit comments