github.com/grafana/tanka@v0.26.1-0.20240506093700-c22cfc35c21a/pkg/process/data_test.go (about)

     1  package process
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"path/filepath"
     7  
     8  	"github.com/grafana/tanka/pkg/jsonnet/implementations/goimpl"
     9  	"github.com/grafana/tanka/pkg/kubernetes/manifest"
    10  )
    11  
    12  // testData holds data for tests
    13  type testData struct {
    14  	Deep interface{}                  `json:"deep"`
    15  	Flat map[string]manifest.Manifest `json:"flat"`
    16  }
    17  
    18  func loadFixture(name string) testData {
    19  	filename := filepath.Join("./testdata", name)
    20  
    21  	vm := goimpl.MakeRawVM([]string{"./testdata"}, nil, nil, 0)
    22  
    23  	data, err := vm.EvaluateFile(filename)
    24  	if err != nil {
    25  		panic(fmt.Sprint("loading fixture:", err))
    26  	}
    27  
    28  	var d testData
    29  	if err := json.Unmarshal([]byte(data), &d); err != nil {
    30  		panic(fmt.Sprint("loading fixture:", err))
    31  	}
    32  
    33  	return d
    34  }
    35  
    36  // testDataRegular is a regular output of jsonnet without special things, but it
    37  // is nested.
    38  func testDataRegular() testData {
    39  	return loadFixture("tdRegular.jsonnet")
    40  }
    41  
    42  // testDataFlat is a flat manifest that does not need reconciliation
    43  func testDataFlat() testData {
    44  	return loadFixture("tdFlat.jsonnet")
    45  }
    46  
    47  // testDataPrimitive is an invalid manifest, because it ends with a primitive
    48  // without including required fields
    49  func testDataPrimitive() testData {
    50  	return loadFixture("tdInvalidPrimitive.jsonnet")
    51  }
    52  
    53  // testBadKindType is an invalid manifest, because it has an invalid `kind` value
    54  func testBadKindType() testData {
    55  	return loadFixture("tdBadKindType.jsonnet")
    56  }
    57  
    58  // testMissingAttribute is an invalid manifest, because it is missing the `kind`
    59  func testMissingAttribute() testData {
    60  	return loadFixture("tdMissingAttribute.jsonnet")
    61  }
    62  
    63  // testDataDeep is super deeply nested on multiple levels
    64  func testDataDeep() testData {
    65  	return loadFixture("tdDeep.jsonnet")
    66  }
    67  
    68  // testDataArray is an array of (deeply nested) dicts that should be fully
    69  // flattened
    70  func testDataArray() testData {
    71  	return loadFixture("tdArray.jsonnet")
    72  }