...

Source file src/edge-infra.dev/pkg/f8n/devinfra/middlechild/middlechild_test.go

Documentation: edge-infra.dev/pkg/f8n/devinfra/middlechild

     1  package middlechild
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"sort"
     7  	"testing"
     8  	"time"
     9  
    10  	"github.com/google/uuid"
    11  	"gotest.tools/v3/assert"
    12  
    13  	"edge-infra.dev/pkg/f8n/devinfra/testinfra/model"
    14  )
    15  
    16  func TestGetArgoPodName(t *testing.T) {
    17  	tfe := []struct {
    18  		job   *Job
    19  		found bool
    20  		pod   []string
    21  	}{
    22  		{
    23  			&Job{
    24  				Metadata: []model.EdgeJobMetadata{
    25  					{Key: "some", Value: "a"},
    26  				},
    27  			},
    28  			false,
    29  			[]string{},
    30  		},
    31  		{
    32  			&Job{
    33  				Metadata: []model.EdgeJobMetadata{
    34  					{Key: "some", Value: "a"},
    35  					{Key: "ID", Value: "hourly-l2-serial-test-1725372000-2227150978"},
    36  				},
    37  			},
    38  			true,
    39  			[]string{"hourly-l2-serial-test-1725372000-rosa-2227150978", "hourly-l2-serial-test-1725372000-rosa-with-kubeconfig-2227150978"},
    40  		},
    41  		{
    42  			&Job{
    43  				Metadata: []model.EdgeJobMetadata{
    44  					{Key: "some", Value: "a"},
    45  					{Key: "ID", Value: ""},
    46  				},
    47  			},
    48  			true,
    49  			[]string{"", ""},
    50  		},
    51  		{
    52  			&Job{
    53  				Metadata: []model.EdgeJobMetadata{
    54  					{Key: "some", Value: "a"},
    55  					{Key: "ID", Value: "1-2"},
    56  				},
    57  			},
    58  			true,
    59  			[]string{"1-rosa-2", "1-rosa-with-kubeconfig-2"},
    60  		},
    61  		{
    62  			&Job{
    63  				Metadata: []model.EdgeJobMetadata{
    64  					{Key: "some", Value: "a"},
    65  					{Key: "ID", Value: "12"},
    66  				},
    67  			},
    68  			true,
    69  			[]string{"12", "12"},
    70  		},
    71  	}
    72  
    73  	for i, tc := range tfe {
    74  		t.Run(fmt.Sprintf("%s_%d", t.Name(), i), func(t *testing.T) {
    75  			found, pod := getArgoPodName(tc.job)
    76  			assert.Equal(t, tc.found, found)
    77  			if tc.found {
    78  				assert.DeepEqual(t, tc.pod, pod)
    79  			}
    80  		})
    81  	}
    82  }
    83  
    84  func TestBuildArgoLogPath(t *testing.T) {
    85  	tfe := []struct {
    86  		job  *Job
    87  		id   string
    88  		path string
    89  	}{
    90  		{
    91  			&Job{
    92  				JobData: model.EdgeJob{
    93  					Run:     "12345678-abcd",
    94  					Started: time.Unix(1725887349, 0).UTC(),
    95  				},
    96  			},
    97  			"hourly-l2-serial-test-1-rosa-2",
    98  			"2024/09/09/12345678-abcd/hourly-l2-serial-test-1-rosa-2/main.log",
    99  		},
   100  		{
   101  			&Job{
   102  				JobData: model.EdgeJob{
   103  					Run:     "12345678-abcd",
   104  					Started: time.Unix(1725973749, 0).UTC(),
   105  				},
   106  			},
   107  			"hourly-l2-serial-test-1-rosa-2",
   108  			"2024/09/10/12345678-abcd/hourly-l2-serial-test-1-rosa-2/main.log",
   109  		},
   110  	}
   111  
   112  	for i, tc := range tfe {
   113  		t.Run(fmt.Sprintf("%s_%d", t.Name(), i), func(t *testing.T) {
   114  			path := buildArgoLogPath(tc.job, tc.id)
   115  			assert.Equal(t, tc.path, path)
   116  		})
   117  	}
   118  }
   119  
   120  func TestBuildArgoResultPath(t *testing.T) {
   121  	tfe := []struct {
   122  		job  *Job
   123  		path string
   124  	}{
   125  		{
   126  			&Job{
   127  				JobData: model.EdgeJob{
   128  					Workflow: "hourly-slow-l2-test",
   129  					Number:   "39998649-1be4-4103-af4a-6f18f1f19aea-AE85iO",
   130  				},
   131  			},
   132  			"argo/edge-infra/hourly-slow-l2-test/39998649-1be4-4103-af4a-6f18f1f19aea-AE85iO/logs/logs.txt",
   133  		},
   134  	}
   135  
   136  	for i, tc := range tfe {
   137  		t.Run(fmt.Sprintf("%s_%d", t.Name(), i), func(t *testing.T) {
   138  			path := buildArgoResultPath(tc.job)
   139  			assert.Equal(t, tc.path, path)
   140  		})
   141  	}
   142  }
   143  
   144  func TestGatherFinished(t *testing.T) {
   145  	fjson := []byte(`{"timestamp":1635278696, "passed": true,"metadata":{"job_name":"build","workflow_name":"CI", "run_id": "2053035745", "platform": "actions"}}`)
   146  
   147  	mc := MiddleChild{}
   148  	mcj := &Job{}
   149  
   150  	mcj.files.JSON = make(map[string][]byte)
   151  	mcj.files.JSON["finished.json"] = fjson
   152  
   153  	assert.NilError(t, mc.gatherFinished(mcj), "failed to gather finished data")
   154  
   155  	t.Logf("%+v", mcj.Metadata)
   156  
   157  	b := []byte{
   158  		0x0, 0x0, 0x0, 0x0,
   159  		0x0, 0x0, 0x0, 0x0,
   160  		0x0, 0x0, 0x0, 0x0,
   161  		0x0, 0x0, 0x0, 0x0,
   162  	}
   163  
   164  	uuid, err := uuid.FromBytes(b)
   165  	assert.NilError(t, err, "failed to create uuid")
   166  
   167  	meta := []model.EdgeJobMetadata{
   168  		{
   169  			EdgeJob: uuid,
   170  			Key:     "job_name",
   171  			Value:   "build",
   172  		},
   173  		{
   174  			EdgeJob: uuid,
   175  			Key:     "workflow_name",
   176  			Value:   "CI",
   177  		},
   178  		{
   179  			EdgeJob: uuid,
   180  			Key:     "run_id",
   181  			Value:   "2053035745",
   182  		},
   183  		{
   184  			EdgeJob: uuid,
   185  			Key:     "platform",
   186  			Value:   "actions",
   187  		},
   188  	}
   189  
   190  	// sort the two structs because theyre not always equal
   191  	sort.Slice(meta, func(i, j int) bool {
   192  		return meta[i].Key < meta[j].Key
   193  	})
   194  
   195  	sort.Slice(mcj.Metadata, func(i, j int) bool {
   196  		return mcj.Metadata[i].Key < mcj.Metadata[j].Key
   197  	})
   198  
   199  	assert.DeepEqual(t, meta, mcj.Metadata)
   200  }
   201  
   202  // func TestGatherJSON(t *testing.T) {
   203  // 	fjson := []byte(`{"timestamp":1635278696, "passed": true,"metadata":{"job_name":"build","workflow_name":"CI", "run_id": "2053035745"}}`)
   204  // 	sjson := []byte(`{"timestamp":1635278593,"machine":"edge-runner","pull":"1244","commit":"da5057b453da76a40f9b65a4e97303fb4f9bc10a","repo":"edge-infra","version":"0.2.0--62135596800"}`)
   205  
   206  // 	mcj := &Job{}
   207  // 	mcj.files.JSON = make(map[string][]byte)
   208  // 	mcj.files.JSON["finished.json"] = fjson
   209  // 	mcj.files.JSON["started.json"] = sjson
   210  
   211  // 	err := mcj.gatherStarted()
   212  // 	if err != nil {
   213  // 		t.Error("Failed to unmarshal started json into JobData struct", err)
   214  // 	}
   215  
   216  // 	st, _ := time.Parse(time.RFC3339, "2021-10-26T16:03:13-04:00")
   217  // 	ft, _ := time.Parse(time.RFC3339, "0001-01-01T00:00:00-04:00")
   218  
   219  // 	var testArr = bigquery.JobData{
   220  // 		Elapsed:  0,
   221  // 		Started:  st.UTC(),
   222  // 		Finished: ft.UTC(),
   223  // 		Version:  "0.2.0--62135596800",
   224  // 		Path:     "",
   225  // 		Job:      "CI/build",
   226  // 		Number:   2053035745,
   227  // 		Metadata: []bigquery.Metadata{
   228  // 			{Key: "pull", Value: "1244"},
   229  // 		},
   230  // 		TestsRun:    0,
   231  // 		TestsFailed: 0,
   232  // 		Passed:      false,
   233  // 		Repos:       "edge-infra",
   234  // 		RepoCommit:  "da5057b453da76a40f9b65a4e97303fb4f9bc10a",
   235  // 	}
   236  
   237  // 	if reflect.DeepEqual(mcj.JobData, testArr) {
   238  // 		t.Error("Test list is incorrect", err)
   239  // 	}
   240  
   241  // 	err = mcj.gatherFinished()
   242  // 	if err != nil {
   243  // 		t.Error("Failed to unmarshal finished json into JobData struct", err)
   244  // 	}
   245  
   246  // 	ft, _ = time.Parse(time.RFC3339, "2021-10-26T16:04:56-04:00")
   247  // 	testArr = bigquery.JobData{
   248  // 		Elapsed:  103,
   249  // 		Started:  st.UTC(),
   250  // 		Finished: ft.UTC(),
   251  // 		Version:  "0.2.0--62135596800",
   252  // 		Path:     "",
   253  // 		Job:      "CI/build",
   254  // 		Number:   2053035745,
   255  // 		Metadata: []bigquery.Metadata{
   256  // 			{Key: "pull", Value: "1244"},
   257  // 			{Key: "job_name", Value: "build"},
   258  // 			{Key: "workflow_name", Value: "CI"},
   259  // 		},
   260  // 		TestsRun:    0,
   261  // 		TestsFailed: 0,
   262  // 		Passed:      true,
   263  // 		Repos:       "edge-infra",
   264  // 		RepoCommit:  "da5057b453da76a40f9b65a4e97303fb4f9bc10a",
   265  // 	}
   266  
   267  // 	if !reflect.DeepEqual(mcj.JobData, testArr) {
   268  // 		t.Error("Test list is incorrect", err)
   269  // 	}
   270  // }
   271  
   272  func TestParseJUnit(t *testing.T) {
   273  	tfe := []struct {
   274  		path     string
   275  		tests    int
   276  		failed   int
   277  		checkJob bool
   278  		job      []model.EdgeJobTest
   279  	}{
   280  		{
   281  			"testdata/unit2558878622.xml", 47, 3, false, []model.EdgeJobTest{},
   282  		},
   283  		{
   284  			"testdata/unit2598106678.xml", 82, 5, false, []model.EdgeJobTest{},
   285  		},
   286  		{
   287  			"testdata/unit1027837568.xml", 18, 0, false, []model.EdgeJobTest{},
   288  		},
   289  		{
   290  			"testdata/unit540587877.xml", 21, 0, false, []model.EdgeJobTest{},
   291  		},
   292  		{
   293  			"testdata/passed.xml", 3, 0, true, []model.EdgeJobTest{
   294  				{Name: "TestDeploymentToLivenessReadinessResponse", Time: 0, Failed: false, FailureText: "", Suite: "edge-infra.dev/pkg/edge/api/graph/mapper"},
   295  				{Name: "TestDeploymentToLivenessReadinessResponseDaemonSet", Time: 0, Failed: false, FailureText: "", Suite: "edge-infra.dev/pkg/edge/api/graph/mapper"},
   296  				{Name: "TestIsClusterReadyForDeletedCluster", Time: 0, Failed: false, FailureText: "", Suite: "edge-infra.dev/pkg/edge/api/graph/mapper"},
   297  			},
   298  		},
   299  		{
   300  			"testdata/failed.xml", 3, 1, true, []model.EdgeJobTest{
   301  				{Name: "TestDeploymentToLivenessReadinessResponse", Time: 0, Failed: true, FailureText: "Assertion failed", Suite: "edge-infra.dev/pkg/edge/api/graph/mapper"},
   302  				{Name: "TestDeploymentToLivenessReadinessResponseDaemonSet", Time: 0, Failed: false, FailureText: "", Suite: "edge-infra.dev/pkg/edge/api/graph/mapper"},
   303  				{Name: "TestIsClusterReadyForDeletedCluster", Time: 0, Failed: false, FailureText: "", Suite: "edge-infra.dev/pkg/edge/api/graph/mapper"},
   304  			},
   305  		},
   306  	}
   307  
   308  	for i, tc := range tfe {
   309  		t.Run(fmt.Sprintf("%s_%d", t.Name(), i), func(t *testing.T) {
   310  			dat, err := os.ReadFile(tc.path)
   311  			assert.NilError(t, err, "failed to read xml file")
   312  
   313  			job, tests, failed, err := parseJUnit(dat)
   314  			assert.NilError(t, err, "failed to parse junit")
   315  			assert.Equal(t, tc.tests, tests)
   316  			assert.Equal(t, tc.failed, failed)
   317  
   318  			if tc.checkJob {
   319  				assert.DeepEqual(t, tc.job, job)
   320  			}
   321  		})
   322  	}
   323  }
   324  

View as plain text