github.com/zntrio/harp/v2@v2.0.9/pkg/bundle/template/visitor/secretbuilder/api_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 secretbuilder
    19  
    20  import (
    21  	"testing"
    22  
    23  	fuzz "github.com/google/gofuzz"
    24  
    25  	bundlev1 "github.com/zntrio/harp/v2/api/gen/go/harp/bundle/v1"
    26  	"github.com/zntrio/harp/v2/pkg/template/engine"
    27  )
    28  
    29  func TestVisit_Fuzz(t *testing.T) {
    30  	// Making sure the descrption never panics
    31  	for i := 0; i < 50; i++ {
    32  		f := fuzz.New()
    33  
    34  		v := New(&bundlev1.Bundle{}, engine.NewContext())
    35  
    36  		tmpl := &bundlev1.Template{
    37  			ApiVersion: "harp.elastic.co/v1",
    38  			Kind:       "BundleTemplate",
    39  			Spec: &bundlev1.TemplateSpec{
    40  				Selector: &bundlev1.Selector{
    41  					Quality:   "production",
    42  					Product:   "harp",
    43  					Version:   "v1.0.0",
    44  					Platform:  "test",
    45  					Component: "cli",
    46  				},
    47  				Namespaces: &bundlev1.Namespaces{},
    48  			},
    49  		}
    50  
    51  		// Infrastructure
    52  		f.Fuzz(&tmpl.Spec.Namespaces.Infrastructure)
    53  		// Platform
    54  		f.Fuzz(&tmpl.Spec.Namespaces.Platform)
    55  		// Product
    56  		f.Fuzz(&tmpl.Spec.Namespaces.Product)
    57  		// Application
    58  		f.Fuzz(&tmpl.Spec.Namespaces.Application)
    59  
    60  		v.Visit(tmpl)
    61  	}
    62  }
    63  
    64  func TestVisit_Template_Fuzz(t *testing.T) {
    65  	// Making sure the descrption never panics
    66  	for i := 0; i < 50; i++ {
    67  		f := fuzz.New()
    68  
    69  		v := New(&bundlev1.Bundle{}, engine.NewContext())
    70  
    71  		tmpl := &bundlev1.Template{
    72  			ApiVersion: "harp.elastic.co/v1",
    73  			Kind:       "BundleTemplate",
    74  			Spec: &bundlev1.TemplateSpec{
    75  				Selector: &bundlev1.Selector{
    76  					Quality:   "production",
    77  					Product:   "harp",
    78  					Version:   "v1.0.0",
    79  					Platform:  "test",
    80  					Component: "cli",
    81  				},
    82  				Namespaces: &bundlev1.Namespaces{
    83  					Infrastructure: []*bundlev1.InfrastructureNS{
    84  						{
    85  							Provider: "aws",
    86  							Account:  "test",
    87  							Regions: []*bundlev1.InfrastructureRegionNS{
    88  								{
    89  									Name: "eu-central-1",
    90  									Services: []*bundlev1.InfrastructureServiceNS{
    91  										{
    92  											Name: "ssh",
    93  											Type: "ec2",
    94  											Secrets: []*bundlev1.SecretSuffix{
    95  												{
    96  													Suffix: "test",
    97  												},
    98  											},
    99  										},
   100  									},
   101  								},
   102  							},
   103  						},
   104  					},
   105  					Platform: []*bundlev1.PlatformRegionNS{
   106  						{
   107  							Region: "eu-central-1",
   108  							Components: []*bundlev1.PlatformComponentNS{
   109  								{
   110  									Type: "rds",
   111  									Name: "postgres-1",
   112  									Secrets: []*bundlev1.SecretSuffix{
   113  										{
   114  											Suffix: "test",
   115  										},
   116  									},
   117  								},
   118  							},
   119  						},
   120  					},
   121  					Product: []*bundlev1.ProductComponentNS{
   122  						{
   123  							Type: "service",
   124  							Name: "rest-api",
   125  							Secrets: []*bundlev1.SecretSuffix{
   126  								{
   127  									Suffix: "test",
   128  								},
   129  							},
   130  						},
   131  					},
   132  					Application: []*bundlev1.ApplicationComponentNS{
   133  						{
   134  							Type: "service",
   135  							Name: "web",
   136  							Secrets: []*bundlev1.SecretSuffix{
   137  								{
   138  									Suffix: "test",
   139  								},
   140  							},
   141  						},
   142  					},
   143  				},
   144  			},
   145  		}
   146  
   147  		// Infrastructure
   148  		f.Fuzz(&tmpl.Spec.Namespaces.Infrastructure[0].Regions[0].Services[0].Secrets[0].Template)
   149  		// Platform
   150  		f.Fuzz(&tmpl.Spec.Namespaces.Platform[0].Components[0].Secrets[0].Template)
   151  		// Product
   152  		f.Fuzz(&tmpl.Spec.Namespaces.Product[0].Secrets[0].Template)
   153  		// Application
   154  		f.Fuzz(&tmpl.Spec.Namespaces.Application[0].Secrets[0].Template)
   155  
   156  		v.Visit(tmpl)
   157  	}
   158  }
   159  
   160  func TestVisit_Content_Fuzz(t *testing.T) {
   161  	// Making sure the descrption never panics
   162  	for i := 0; i < 50; i++ {
   163  		f := fuzz.New().NilChance(0).NumElements(1, 5)
   164  
   165  		v := New(&bundlev1.Bundle{}, engine.NewContext())
   166  
   167  		tmpl := &bundlev1.Template{
   168  			ApiVersion: "harp.elastic.co/v1",
   169  			Kind:       "BundleTemplate",
   170  			Spec: &bundlev1.TemplateSpec{
   171  				Selector: &bundlev1.Selector{
   172  					Quality:   "production",
   173  					Product:   "harp",
   174  					Version:   "v1.0.0",
   175  					Platform:  "test",
   176  					Component: "cli",
   177  				},
   178  				Namespaces: &bundlev1.Namespaces{
   179  					Infrastructure: []*bundlev1.InfrastructureNS{
   180  						{
   181  							Provider: "aws",
   182  							Account:  "test",
   183  							Regions: []*bundlev1.InfrastructureRegionNS{
   184  								{
   185  									Name: "eu-central-1",
   186  									Services: []*bundlev1.InfrastructureServiceNS{
   187  										{
   188  											Name: "ssh",
   189  											Type: "ec2",
   190  											Secrets: []*bundlev1.SecretSuffix{
   191  												{
   192  													Suffix:  "test",
   193  													Content: map[string]string{},
   194  												},
   195  											},
   196  										},
   197  									},
   198  								},
   199  							},
   200  						},
   201  					},
   202  					Platform: []*bundlev1.PlatformRegionNS{
   203  						{
   204  							Region: "eu-central-1",
   205  							Components: []*bundlev1.PlatformComponentNS{
   206  								{
   207  									Type: "rds",
   208  									Name: "postgres-1",
   209  									Secrets: []*bundlev1.SecretSuffix{
   210  										{
   211  											Suffix:  "test",
   212  											Content: map[string]string{},
   213  										},
   214  									},
   215  								},
   216  							},
   217  						},
   218  					},
   219  					Product: []*bundlev1.ProductComponentNS{
   220  						{
   221  							Type: "service",
   222  							Name: "rest-api",
   223  							Secrets: []*bundlev1.SecretSuffix{
   224  								{
   225  									Suffix:  "test",
   226  									Content: map[string]string{},
   227  								},
   228  							},
   229  						},
   230  					},
   231  					Application: []*bundlev1.ApplicationComponentNS{
   232  						{
   233  							Type: "service",
   234  							Name: "web",
   235  							Secrets: []*bundlev1.SecretSuffix{
   236  								{
   237  									Suffix:  "test",
   238  									Content: map[string]string{},
   239  								},
   240  							},
   241  						},
   242  					},
   243  				},
   244  			},
   245  		}
   246  
   247  		// Infrastructure
   248  		f.Fuzz(&tmpl.Spec.Namespaces.Infrastructure[0].Regions[0].Services[0].Secrets[0].Content)
   249  		// Platform
   250  		f.Fuzz(&tmpl.Spec.Namespaces.Platform[0].Components[0].Secrets[0].Content)
   251  		// Product
   252  		f.Fuzz(&tmpl.Spec.Namespaces.Product[0].Secrets[0].Content)
   253  		// Application
   254  		f.Fuzz(&tmpl.Spec.Namespaces.Application[0].Secrets[0].Content)
   255  
   256  		v.Visit(tmpl)
   257  	}
   258  }