...

Source file src/edge-infra.dev/cmd/tools/art/argo/argo_test.go

Documentation: edge-infra.dev/cmd/tools/art/argo

     1  package argo
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/json"
     6  	"fmt"
     7  	"os"
     8  	"testing"
     9  
    10  	"github.com/stretchr/testify/assert"
    11  )
    12  
    13  func TestGenerateStarted(t *testing.T) {
    14  	tests := []struct {
    15  		argo     *argo
    16  		expected string
    17  	}{
    18  		{
    19  			&argo{
    20  				startedTimestamp:  "2024-08-09T19:00:07Z",
    21  				machine:           "edge-runner-2",
    22  				pull:              "10272",
    23  				commit:            "c4d41a9f151695a417b523cf56b250289ffd9152",
    24  				repo:              "edge-infra",
    25  				rosaVersionString: "0.20.0+commit.c4d41a9",
    26  			},
    27  			"testdata/started.json",
    28  		},
    29  	}
    30  
    31  	for i, test := range tests {
    32  		argo := &argo{}
    33  		t.Run(fmt.Sprintf("%s_%d", t.Name(), i), func(t *testing.T) {
    34  			argo = test.argo
    35  			result, err := argo.generateStarted()
    36  			assert.NoError(t, err)
    37  
    38  			// pretty print the json string
    39  			dst := &bytes.Buffer{}
    40  			err = json.Indent(dst, []byte(result), "", "  ")
    41  			assert.NoError(t, err)
    42  
    43  			// file contents should already be pretty
    44  			dat, err := os.ReadFile(test.expected)
    45  			assert.NoError(t, err)
    46  
    47  			assert.EqualValues(t, dst.String(), string(dat))
    48  		})
    49  	}
    50  }
    51  
    52  func TestGenerateFinished(t *testing.T) {
    53  	tests := []struct {
    54  		argo     *argo
    55  		expected string
    56  	}{
    57  		{
    58  			&argo{
    59  				finishedTimestamp: "2024-08-09T19:10:07Z",
    60  				passed:            "Succeeded",
    61  				machine:           "vroom-vroom-machine",
    62  				runID:             "123",
    63  				workflow:          "hourly-stable",
    64  				metadata:          `{"clusterType":"gke","clusterVersion":"1.28.10-gke"}`,
    65  			},
    66  			"testdata/finished.json",
    67  		},
    68  	}
    69  
    70  	for i, test := range tests {
    71  		argo := &argo{}
    72  		t.Run(fmt.Sprintf("%s_%d", t.Name(), i), func(t *testing.T) {
    73  			argo = test.argo
    74  			result, err := argo.generateFinished("123-ab45c")
    75  			assert.NoError(t, err)
    76  
    77  			// pretty print the json string
    78  			dst := &bytes.Buffer{}
    79  			err = json.Indent(dst, []byte(result), "", "  ")
    80  			assert.NoError(t, err)
    81  
    82  			// file contents should already be pretty
    83  			dat, err := os.ReadFile(test.expected)
    84  			assert.NoError(t, err)
    85  
    86  			assert.EqualValues(t, dst.String(), string(dat))
    87  		})
    88  	}
    89  }
    90  

View as plain text