github.com/zntrio/harp/v2@v2.0.9/pkg/bundle/template/executor_test.go (about)

     1  // Licensed to Elasticsearch B.V. under one or more contributor
     2  // license agreements. See the NOTICE file distributed with
     3  // this work for additional information regarding copyright
     4  // ownership. Elasticsearch B.V. licenses this file to you under
     5  // the Apache License, Version 2.0 (the "License"); you may
     6  // not use this file except in compliance with the License.
     7  // You may obtain a copy of the License at
     8  //
     9  //     http://www.apache.org/licenses/LICENSE-2.0
    10  //
    11  // Unless required by applicable law or agreed to in writing,
    12  // software distributed under the License is distributed on an
    13  // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    14  // KIND, either express or implied.  See the License for the
    15  // specific language governing permissions and limitations
    16  // under the License.
    17  
    18  package template
    19  
    20  import (
    21  	"reflect"
    22  	"testing"
    23  
    24  	fuzz "github.com/google/gofuzz"
    25  
    26  	bundlev1 "github.com/zntrio/harp/v2/api/gen/go/harp/bundle/v1"
    27  	"github.com/zntrio/harp/v2/pkg/bundle/template/visitor/secretbuilder"
    28  	"github.com/zntrio/harp/v2/pkg/template/engine"
    29  )
    30  
    31  func TestValidate(t *testing.T) {
    32  	type args struct {
    33  		spec *bundlev1.Template
    34  	}
    35  	tests := []struct {
    36  		name    string
    37  		args    args
    38  		wantErr bool
    39  	}{
    40  		{
    41  			name:    "nil",
    42  			wantErr: true,
    43  		},
    44  		{
    45  			name: "invalid apiVersion",
    46  			args: args{
    47  				spec: &bundlev1.Template{
    48  					ApiVersion: "foo",
    49  				},
    50  			},
    51  			wantErr: true,
    52  		},
    53  		{
    54  			name: "invalid kind",
    55  			args: args{
    56  				spec: &bundlev1.Template{
    57  					ApiVersion: "harp.elastic.co/v1",
    58  					Kind:       "foo",
    59  				},
    60  			},
    61  			wantErr: true,
    62  		},
    63  		{
    64  			name: "nil meta",
    65  			args: args{
    66  				spec: &bundlev1.Template{
    67  					ApiVersion: "harp.elastic.co/v1",
    68  					Kind:       "BundleTemplate",
    69  				},
    70  			},
    71  			wantErr: true,
    72  		},
    73  		{
    74  			name: "meta name not defined",
    75  			args: args{
    76  				spec: &bundlev1.Template{
    77  					ApiVersion: "harp.elastic.co/v1",
    78  					Kind:       "BundleTemplate",
    79  					Meta:       &bundlev1.TemplateMeta{},
    80  				},
    81  			},
    82  			wantErr: true,
    83  		},
    84  		{
    85  			name: "nil spec",
    86  			args: args{
    87  				spec: &bundlev1.Template{
    88  					ApiVersion: "harp.elastic.co/v1",
    89  					Kind:       "BundleTemplate",
    90  					Meta:       &bundlev1.TemplateMeta{},
    91  				},
    92  			},
    93  			wantErr: true,
    94  		},
    95  		{
    96  			name: "no action template",
    97  			args: args{
    98  				spec: &bundlev1.Template{
    99  					ApiVersion: "harp.elastic.co/v1",
   100  					Kind:       "BundleTemplate",
   101  					Meta:       &bundlev1.TemplateMeta{},
   102  					Spec:       &bundlev1.TemplateSpec{},
   103  				},
   104  			},
   105  			wantErr: false,
   106  		},
   107  	}
   108  	for _, tt := range tests {
   109  		t.Run(tt.name, func(t *testing.T) {
   110  			if err := Validate(tt.args.spec); (err != nil) != tt.wantErr {
   111  				t.Errorf("Validate() error = %v, wantErr %v", err, tt.wantErr)
   112  			}
   113  		})
   114  	}
   115  }
   116  
   117  func TestChecksum(t *testing.T) {
   118  	type args struct {
   119  		spec *bundlev1.Template
   120  	}
   121  	tests := []struct {
   122  		name    string
   123  		args    args
   124  		want    string
   125  		wantErr bool
   126  	}{
   127  		{
   128  			name:    "nil",
   129  			wantErr: true,
   130  		},
   131  		{
   132  			name: "valid",
   133  			args: args{
   134  				spec: &bundlev1.Template{
   135  					ApiVersion: "harp.elastic.co/v1",
   136  					Kind:       "BundleTemplate",
   137  					Meta:       &bundlev1.TemplateMeta{},
   138  					Spec:       &bundlev1.TemplateSpec{},
   139  				},
   140  			},
   141  			wantErr: false,
   142  			want:    "qnYJsLsuawKi7c3A4mgrOm9akKKdt57NJdRR92xlOfA",
   143  		},
   144  	}
   145  	for _, tt := range tests {
   146  		t.Run(tt.name, func(t *testing.T) {
   147  			got, err := Checksum(tt.args.spec)
   148  			if (err != nil) != tt.wantErr {
   149  				t.Errorf("Checksum() error = %v, wantErr %v", err, tt.wantErr)
   150  				return
   151  			}
   152  			if !reflect.DeepEqual(got, tt.want) {
   153  				t.Errorf("Checksum() = %v, want %v", got, tt.want)
   154  			}
   155  		})
   156  	}
   157  }
   158  
   159  func TestExecute_Fuzz(t *testing.T) {
   160  	// Making sure the descrption never panics
   161  	for i := 0; i < 50; i++ {
   162  		f := fuzz.New()
   163  
   164  		// Prepare arguments
   165  		spec := &bundlev1.Template{
   166  			ApiVersion: "harp.elastic.co/v1",
   167  			Kind:       "BundleTemplate",
   168  			Meta:       &bundlev1.TemplateMeta{},
   169  			Spec:       &bundlev1.TemplateSpec{},
   170  		}
   171  
   172  		// Fuzz input
   173  		f.Fuzz(&spec.Spec.Namespaces)
   174  
   175  		// Initialize a bundle creator
   176  		var b *bundlev1.Bundle
   177  		v := secretbuilder.New(b, engine.NewContext())
   178  
   179  		// Execute
   180  		Execute(spec, v)
   181  	}
   182  }