github.com/zppinho/prow@v0.0.0-20240510014325-1738badeb017/cmd/cm2kc/main_test.go (about)

     1  /*
     2  Copyright 2020 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package main
    18  
    19  import (
    20  	"bytes"
    21  	"os"
    22  	"path/filepath"
    23  	"strings"
    24  	"testing"
    25  
    26  	"github.com/spf13/pflag"
    27  )
    28  
    29  const (
    30  	testDir = "testdata"
    31  )
    32  
    33  func resolvePath(t *testing.T, filename string) string {
    34  	name := strings.ToLower(filepath.Base(t.Name()))
    35  	return filepath.Join(testDir, strings.ToLower(name), name+filename)
    36  }
    37  
    38  func TestGenjobs(t *testing.T) {
    39  	tests := []struct {
    40  		name   string
    41  		output string
    42  		args   []string
    43  		equal  bool
    44  	}{
    45  		{
    46  			name:  "single cluster",
    47  			args:  []string{},
    48  			equal: true,
    49  		},
    50  		{
    51  			name:  "multiple clusters",
    52  			args:  []string{},
    53  			equal: true,
    54  		},
    55  	}
    56  
    57  	for _, test := range tests {
    58  		t.Run(test.name, func(t *testing.T) {
    59  			in := resolvePath(t, "_in.yaml")
    60  			outE := resolvePath(t, "_out.yaml")
    61  
    62  			expected, err := os.ReadFile(outE)
    63  			if err != nil {
    64  				t.Errorf("Failed reading expected output file %v: %v", outE, err)
    65  			}
    66  
    67  			tmpDir := t.TempDir()
    68  			outA := filepath.Join(tmpDir, "out.yaml")
    69  
    70  			os.Args = []string{"cm2kc"}
    71  			pflag.CommandLine = pflag.NewFlagSet(os.Args[0], pflag.ExitOnError)
    72  			os.Args = append(os.Args, test.args...)
    73  			os.Args = append(os.Args, "--input="+in, "--output="+outA)
    74  			main()
    75  
    76  			actual, err := os.ReadFile(outA)
    77  			if err != nil {
    78  				t.Errorf("Failed reading actual output file %v: %v", outA, err)
    79  			}
    80  
    81  			t.Logf("expected (%v):\n%v\n", test.name, string(expected))
    82  			t.Logf("actual (%v):\n%v\n", test.name, string(actual))
    83  
    84  			if os.Getenv("REFRESH_GOLDEN") == "true" {
    85  				if err = os.WriteFile(outE, actual, 0644); err != nil {
    86  					t.Errorf("Failed writing expected output file %v: %v", outE, err)
    87  				}
    88  				expected = actual
    89  			}
    90  
    91  			equal := bytes.Equal(expected, actual)
    92  			if equal != test.equal {
    93  				t.Errorf("Expected output equality to be: %t.", test.equal)
    94  			}
    95  		})
    96  	}
    97  }