...

Source file src/edge-infra.dev/test/f2/examples/embed/kustomization/kustomization_test.go

Documentation: edge-infra.dev/test/f2/examples/embed/kustomization

     1  package kustomization_test
     2  
     3  import (
     4  	"bytes"
     5  	"context"
     6  	"embed"
     7  	"encoding/json"
     8  	"fmt"
     9  	"io/fs"
    10  	"os"
    11  	"testing"
    12  
    13  	"gotest.tools/v3/assert"
    14  	"sigs.k8s.io/kustomize/api/filters/namespace"
    15  	"sigs.k8s.io/kustomize/api/types"
    16  	"sigs.k8s.io/kustomize/kyaml/kio"
    17  
    18  	"edge-infra.dev/pkg/k8s/decoder"
    19  	"edge-infra.dev/pkg/k8s/unstructured"
    20  	"edge-infra.dev/test/f2"
    21  	"edge-infra.dev/test/f2/integration"
    22  	"edge-infra.dev/test/f2/x/ktest"
    23  	"edge-infra.dev/test/f2/x/ktest/envtest"
    24  )
    25  
    26  // testdata -- the contents of this direcotory are generated by a genrule
    27  //
    28  //go:embed testdata
    29  var manifests embed.FS
    30  
    31  var f f2.Framework
    32  
    33  func TestMain(m *testing.M) {
    34  	f = f2.New(
    35  		context.Background(),
    36  		f2.WithExtensions(
    37  			ktest.New(
    38  				ktest.WithEnvtestOptions(
    39  					envtest.WithoutCRDs(),
    40  				),
    41  			),
    42  		),
    43  	).
    44  		Setup(func(ctx f2.Context) (f2.Context, error) {
    45  			// Test execution should end here unless -integration-level=2 is passed to test
    46  			if !integration.IsL2() {
    47  				return ctx, fmt.Errorf("%w: requires L2 integration test level", f2.ErrSkip)
    48  			}
    49  
    50  			return ctx, nil
    51  		})
    52  
    53  	os.Exit(f.Run(m))
    54  }
    55  
    56  func TestApplyKustomizationDeployment(t *testing.T) {
    57  	fin := f2.NewFeature("generated kustomization with image sha").
    58  		Test("Deploy kustomization", func(ctx f2.Context, t *testing.T) f2.Context {
    59  			var k = ktest.FromContextT(ctx, t)
    60  
    61  			out, err := processManifests(ctx, t, manifests, "testdata/kustomization_manifests.yaml")
    62  			assert.NilError(t, err)
    63  
    64  			for _, object := range out {
    65  				err := k.Client.Create(ctx, object)
    66  				assert.NilError(t, err)
    67  			}
    68  
    69  			val, err := json.Marshal(out)
    70  			assert.NilError(t, err)
    71  
    72  			fmt.Println(string(val))
    73  
    74  			return ctx
    75  		}).Feature()
    76  
    77  	f.Test(t, fin)
    78  }
    79  
    80  func processManifests(ctx f2.Context, t *testing.T, fs fs.FS, path string) ([]*unstructured.Unstructured, error) {
    81  	buf := bytes.Buffer{}
    82  
    83  	file, err := fs.Open(path)
    84  	if err != nil {
    85  		return nil, fmt.Errorf("error opening file: %w", err)
    86  	}
    87  
    88  	err = kio.Pipeline{
    89  		Inputs:  []kio.Reader{&kio.ByteReader{Reader: file}},
    90  		Outputs: []kio.Writer{kio.ByteWriter{Writer: &buf}},
    91  		Filters: []kio.Filter{processNamespace(ctx, t)},
    92  	}.Execute()
    93  
    94  	if err != nil {
    95  		return nil, fmt.Errorf("failed to process manifests: %w", err)
    96  	}
    97  
    98  	return decoder.DecodeYAML(buf.Bytes())
    99  }
   100  
   101  // processNamespace returns a kio.Filter for updating all namespace references
   102  // in manifests to point to the test specific namespace. Includes updating
   103  // service account's namespaces in role bindings.
   104  func processNamespace(ctx f2.Context, t *testing.T) kio.Filter {
   105  	k := ktest.FromContextT(ctx, t)
   106  
   107  	fss := types.FsSlice{
   108  		{
   109  			Path:               "metadata/namespace",
   110  			CreateIfNotPresent: true,
   111  		},
   112  	}
   113  
   114  	return namespace.Filter{
   115  		Namespace:              k.Namespace,
   116  		FsSlice:                fss,
   117  		SetRoleBindingSubjects: namespace.AllServiceAccountSubjects,
   118  	}
   119  }
   120  

View as plain text