github.com/verrazzano/verrazzano@v1.7.0/tools/generate-profiles/generate_test.go (about)

     1  // Copyright (c) 2022, Oracle and/or its affiliates.
     2  // Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
     3  package main
     4  
     5  import (
     6  	"fmt"
     7  	"github.com/stretchr/testify/assert"
     8  	"github.com/verrazzano/verrazzano/platform-operator/apis/verrazzano/v1beta1"
     9  	"os"
    10  	"sigs.k8s.io/yaml"
    11  	"strings"
    12  	"testing"
    13  )
    14  
    15  var vzDir = "../.."
    16  
    17  // TestRun tests the following scenario
    18  // GIVEN a call to run
    19  // WHEN all the specified args and env vars are set
    20  // THEN a generated profile file is found in the output directory
    21  func TestRun(t *testing.T) {
    22  	assert := assert.New(t)
    23  	os.Setenv(VzRootDir, vzDir)
    24  	dir, err := os.MkdirTemp("", "temp")
    25  	assert.NoError(err)
    26  	defer os.RemoveAll(dir)
    27  	err = run("prod", dir)
    28  	assert.NoError(err)
    29  	_, err = os.Stat(dir + "/" + "prod.yaml")
    30  	assert.NoError(err)
    31  }
    32  
    33  // TestVerrazzanoRootNotSpecified tests the following scenario
    34  // GIVEN a call to run func from main func
    35  // WHEN VERRAZZANO_ROOT env var is not specified
    36  // THEN an error is returned
    37  func TestVerrazzanoRootNotSpecified(t *testing.T) {
    38  	assert := assert.New(t)
    39  	os.Unsetenv(VzRootDir)
    40  	err := run("prod", "foo")
    41  	assert.Error(err)
    42  	assert.Equal(err, fmt.Errorf("VERRAZZANO_ROOT environment variable not specified"))
    43  }
    44  
    45  // TestInvalidOutputLocation tests the following scenario
    46  // GIVEN a call to run func
    47  // WHEN the outputLocation is invalid
    48  // THEN an error is returned
    49  func TestInvalidOutputLocation(t *testing.T) {
    50  	assert := assert.New(t)
    51  	os.Setenv(VzRootDir, vzDir)
    52  	err := run("prod", "${HOME}/foo")
    53  	assert.Error(err)
    54  	assert.ErrorContains(err, "foo: no such file or directory")
    55  }
    56  
    57  // TestInvalidProfileType tests the following scenario
    58  // GIVEN a call to generateProfile
    59  // WHEN profile is found to be invalid
    60  // THEN an error is returned containing the message that the profile file was not found
    61  func TestInvalidProfileType(t *testing.T) {
    62  	assert := assert.New(t)
    63  	_, err := generateProfile("foo", vzDir)
    64  	assert.Error(err)
    65  	assert.ErrorContains(err, "foo.yaml: no such file or directory")
    66  }
    67  
    68  // TestValidProfileType tests the following scenario
    69  // GIVEN a call to generate cr of a profileType
    70  // WHEN the profileType is found to be valid
    71  // THEN no error is returned
    72  func TestValidProfileType(t *testing.T) {
    73  	assert := assert.New(t)
    74  	_, err := generateProfile("dev", vzDir)
    75  	assert.NoError(err)
    76  }
    77  
    78  // TestFieldsOmitted tests the following scenario
    79  // GIVEN a call to generate cr of a profileType
    80  // WHEN the profileType is found to be valid
    81  // THEN no error is returned and certain fields
    82  // that were omitted during serialization are not found
    83  func TestFieldsOmitted(t *testing.T) {
    84  	// Expect certain fields to be missing if yaml.Marshal
    85  	// is called on the cr Alias type
    86  	assert := assert.New(t)
    87  	cr, err := generateProfile("dev", vzDir)
    88  	assert.NoError(err)
    89  	crYaml, err := yaml.Marshal(cr)
    90  	assert.NoError(err)
    91  	assert.False(strings.Contains(string(crYaml), "status"))
    92  	assert.False(strings.Contains(string(crYaml), "creationTimestamp"))
    93  	assert.True(strings.Contains(string(crYaml), "spec"))
    94  	assert.True(strings.Contains(string(crYaml), "metadata"))
    95  
    96  	// When yaml.Marshal method is called on an object if type v1beta1.Verrazzano
    97  	// then expect status and creationTimestamp to show up
    98  	vzCR := v1beta1.Verrazzano(*cr)
    99  	vzCRYaml, err := yaml.Marshal(&vzCR)
   100  	assert.NoError(err)
   101  	assert.True(strings.Contains(string(vzCRYaml), "status"))
   102  	assert.True(strings.Contains(string(vzCRYaml), "creationTimestamp"))
   103  }