github.com/ouraigua/jenkins-library@v0.0.0-20231028010029-fbeaf2f3aa9b/pkg/generator/helper/resources_test.go (about)

     1  //go:build unit
     2  // +build unit
     3  
     4  package helper
     5  
     6  import (
     7  	"fmt"
     8  	"testing"
     9  
    10  	"github.com/stretchr/testify/assert"
    11  )
    12  
    13  func TestInfluxResource_StructString(t *testing.T) {
    14  	tt := []struct {
    15  		in       InfluxResource
    16  		expected string
    17  	}{
    18  		{
    19  			in: InfluxResource{
    20  				Name:     "TestInflux",
    21  				StepName: "TestStep",
    22  				Measurements: []InfluxMeasurement{
    23  					{
    24  						Name:   "m1",
    25  						Fields: []InfluxMetric{{Name: "field1_1"}, {Name: "field1_2"}},
    26  						Tags:   []InfluxMetric{{Name: "tag1_1"}, {Name: "tag1_2"}},
    27  					},
    28  					{
    29  						Name:   "m2",
    30  						Fields: []InfluxMetric{{Name: "field2_1"}, {Name: "field2_2"}},
    31  						Tags:   []InfluxMetric{{Name: "tag2_1"}, {Name: "tag2_2"}},
    32  					},
    33  				},
    34  			},
    35  			expected: `type TestStepTestInflux struct {
    36  	m1 struct {
    37  		fields struct {
    38  			field1_1 string
    39  			field1_2 string
    40  		}
    41  		tags struct {
    42  			tag1_1 string
    43  			tag1_2 string
    44  		}
    45  	}
    46  	m2 struct {
    47  		fields struct {
    48  			field2_1 string
    49  			field2_2 string
    50  		}
    51  		tags struct {
    52  			tag2_1 string
    53  			tag2_2 string
    54  		}
    55  	}
    56  }
    57  
    58  func (i *TestStepTestInflux) persist(path, resourceName string) {
    59  	measurementContent := []struct{
    60  		measurement string
    61  		valType     string
    62  		name        string
    63  		value       interface{}
    64  	}{
    65  		{valType: config.InfluxField, measurement: "m1" , name: "field1_1", value: i.m1.fields.field1_1},
    66  		{valType: config.InfluxField, measurement: "m1" , name: "field1_2", value: i.m1.fields.field1_2},
    67  		{valType: config.InfluxTag, measurement: "m1" , name: "tag1_1", value: i.m1.tags.tag1_1},
    68  		{valType: config.InfluxTag, measurement: "m1" , name: "tag1_2", value: i.m1.tags.tag1_2},
    69  		{valType: config.InfluxField, measurement: "m2" , name: "field2_1", value: i.m2.fields.field2_1},
    70  		{valType: config.InfluxField, measurement: "m2" , name: "field2_2", value: i.m2.fields.field2_2},
    71  		{valType: config.InfluxTag, measurement: "m2" , name: "tag2_1", value: i.m2.tags.tag2_1},
    72  		{valType: config.InfluxTag, measurement: "m2" , name: "tag2_2", value: i.m2.tags.tag2_2},
    73  	}
    74  
    75  	errCount := 0
    76  	for _, metric := range measurementContent {
    77  		err := piperenv.SetResourceParameter(path, resourceName, filepath.Join(metric.measurement, fmt.Sprintf("%vs", metric.valType), metric.name), metric.value)
    78  		if err != nil {
    79  			log.Entry().WithError(err).Error("Error persisting influx environment.")
    80  			errCount++
    81  		}
    82  	}
    83  	if errCount > 0 {
    84  		log.Entry().Error("failed to persist Influx environment")
    85  	}
    86  }`,
    87  		},
    88  	}
    89  
    90  	for run, test := range tt {
    91  		t.Run(fmt.Sprintf("Run %v", run), func(t *testing.T) {
    92  			got, err := test.in.StructString()
    93  			assert.NoError(t, err)
    94  			assert.Equal(t, test.expected, got)
    95  		})
    96  
    97  	}
    98  }
    99  
   100  func TestReportsResource_StructString(t *testing.T) {
   101  	tt := []struct {
   102  		in       ReportsResource
   103  		expected string
   104  	}{
   105  		{
   106  			in: ReportsResource{
   107  				Name:     "reports",
   108  				StepName: "testStep",
   109  				Parameters: []ReportsParameter{
   110  					{
   111  						FilePattern: "pattern1",
   112  						Type:        "general",
   113  					},
   114  					{
   115  						FilePattern: "pattern2",
   116  					},
   117  					{
   118  						ParamRef: "testParam",
   119  					},
   120  				},
   121  			},
   122  			expected: `type testStepReports struct {
   123  }
   124  
   125  func (p *testStepReports) persist(stepConfig testStepOptions, gcpJsonKeyFilePath string, gcsBucketId string, gcsFolderPath string, gcsSubFolder string) {
   126  	if gcsBucketId == "" {
   127  		log.Entry().Info("persisting reports to GCS is disabled, because gcsBucketId is empty")
   128  		return
   129  	}
   130  	log.Entry().Info("Uploading reports to Google Cloud Storage...")
   131  	content := []gcs.ReportOutputParam{
   132  		{FilePattern: "pattern1", ParamRef: "", StepResultType: "general"},
   133  		{FilePattern: "pattern2", ParamRef: "", StepResultType: ""},
   134  		{FilePattern: "", ParamRef: "testParam", StepResultType: ""},
   135  	}
   136  	envVars := []gcs.EnvVar{
   137  		{Name: "GOOGLE_APPLICATION_CREDENTIALS", Value: gcpJsonKeyFilePath, Modified: false},
   138  	}
   139  	gcsClient, err := gcs.NewClient(gcs.WithEnvVars(envVars))
   140  	if err != nil {
   141  		log.Entry().Errorf("creation of GCS client failed: %v", err)
   142          	return
   143  	}
   144  	defer gcsClient.Close()
   145  	structVal := reflect.ValueOf(&stepConfig).Elem()
   146  	inputParameters := map[string]string{}
   147  	for i := 0; i < structVal.NumField(); i++ {
   148  		field := structVal.Type().Field(i)
   149  		if field.Type.String() == "string" {
   150  			paramName := strings.Split(field.Tag.Get("json"), ",")
   151  			paramValue, _ := structVal.Field(i).Interface().(string)
   152  			inputParameters[paramName[0]] = paramValue
   153  		}
   154  	}
   155  	if err := gcs.PersistReportsToGCS(gcsClient, content, inputParameters, gcsFolderPath, gcsBucketId, gcsSubFolder, doublestar.Glob, os.Stat); err != nil {
   156  		log.Entry().Errorf("failed to persist reports: %v", err)
   157  	}
   158  }`,
   159  		},
   160  	}
   161  
   162  	for run, test := range tt {
   163  		t.Run(fmt.Sprintf("Run %v", run), func(t *testing.T) {
   164  			got, err := test.in.StructString()
   165  			assert.NoError(t, err)
   166  			assert.Equal(t, test.expected, got)
   167  		})
   168  
   169  	}
   170  }