Upgrade to Pro — share decks privately, control downloads, hide ads and more …

KET / Kind e2e Test framework

riita10069
October 21, 2021

KET / Kind e2e Test framework

https://mercari.connpass.com/event/227024/
で発表しました。

アーカイブはYouTubeから見れます!
https://www.youtube.com/watch?v=u5Eq-uJBvzg

riita10069

October 21, 2021
Tweet

More Decks by riita10069

Other Decks in Technology

Transcript

  1. None
  2. None
  3. Are you testing the Kubernetes Controller?

  4. None
  5. IUUQTTLB ff PMEEFWEPDTQJQFMJOFTUBHFT

  6. #VJME 4LB ff PME ,VCFOFUFTDMVTUFS %FQMPZ 7PMVNFECH *OJU$POUBJOFS %PXOMPBE%FMWF $VTUPN$POUSPMMFS

    $VTUPN$POUSPMMFS $VTUPN$POUSPMMFS https://github.com/go-delve/delve
  7. https://github.com/docker https://github.com/kubernetes-sigs/kind

  8. $ kubectl apply -f test-case-1.yaml Before test After test $

    kubectl delete -f test-case-1.yaml
  9. https://github.com/riita10069/ket

  10. Collecting CLIs Executing Command SetUp Cluster Build/Deploy/Debug Manage Resources

  11. None
  12. func Start(ctx context.Context, options ...Option) (*ClientSet, error)

  13. type ClientSet struct { ClientGo *k8s.ClientG o Kubectl *kubectl.Kubect l

    Kind *kind.Kin d Skaffold *skaffold.Skaffol d } err := kubectl.ApplyAllManifest(ctx, tt.fixture.manifestPaths, false) _, err = kubectl.WaitAResource ( ctx , "deploy" , types.NamespacedName { Namespace: "CONTROLLER_NAMESPACE" , Name: "CONTROLLER_NAME" , } , )
  14. func (k *Kubectl) ApplyFile(ctx context.Context, filePath string) error { args

    := []string { "apply" , "-f" , filePath , } err := k.Execute(ctx, args ) if err != nil { return err } return ni l } Self-created Commands!!
  15. for _, tt := range tests { tt := t

    t t.Run(tt.name, func(t *testing.T) { ctx := context.Background( ) err := kubectl.ApplyAllManifest(ctx, tt.fixture.manifestPaths, false ) for _, expect := range tt.res.A.Items { // check expec t } err = kubectl.DeleteAllManifest(ctx, tt.fixture.manifestPaths, true ) } ) } 1st, Create resources “declaratively” Maybe notify informer of the change. 2nd, Make sure the behavior of the Controller. 3rd, Restore the state of the resource.
  16. How was it implemented?

  17. func Start(ctx context.Context, options ...Option) (*ClientSet, error) { ket :=

    NewKET( ) for _, option := range options { err := option(ket ) if err != nil { return nil, fmt.Errorf("failed to run options: %w", err ) } } cliSet := &ClientSet{ } kind := kind.NewKind(ket.kindVersion, ket.kubernetesVersion, ket.binDir, ket.kubeconfigPath ) cliSet.Kind = kin d err := kind.DeleteCluster(ctx, ket.kindClusterName ) if err != nil { return nil, fmt.Errorf("failed to delete kind cluster %s: %w", ket.kindClusterName, err ) } err = kind.CreateCluster(ctx, ket.kindClusterName ) if err != nil { return nil, fmt.Errorf("failed to create kind cluster %s: %w", ket.kindClusterName, err ) } First, create a Kind cluster.
  18. clientGo, err := k8s.NewClientGo(ket.kubeconfigPath ) if err != nil {

    return nil, fmt.Errorf("failed to create client-go: %w", err ) } cliSet.ClientGo = clientG o kubectl := kubectl.NewKubectl(ket.kubernetesVersion, ket.binDir, ket.kubeconfigPath ) cliSet.Kubectl = kubect l err = kubectl.UseContext(ctx, ket.kindClusterName ) if err != nil { return nil, fmt.Errorf("failed to use context: %w", err ) } err = kubectl.ApplyKustomize(ctx, ket.crdKustomizePath ) if err != nil { return nil, fmt.Errorf("failed to apply crd yaml: %w", err ) } if ket.useSkaffold { skaffold := skaffold.NewSkaffold(ket.skaffoldVersion, ket.binDir, ket.kubeconfigPath ) cliSet.Skaffold = skaffol d err = skaffold.Run(ctx, ket.skaffoldYaml, false ) if err != nil { return nil, fmt.Errorf("failed to skaffold run: %w", err ) } } return cliSet, ni l } Second, Apply CRDs and some Resources. Third, Skaffold run.(if you use Skaffold)
  19. https://github.com/riita10069/ket