github.com/tilt-dev/tilt@v0.33.15-0.20240515162809-0a22ed45d8a0/internal/k8s/serialize_test.go (about)

     1  package k8s
     2  
     3  import (
     4  	"strings"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  
     9  	"github.com/tilt-dev/tilt/internal/k8s/testyaml"
    10  )
    11  
    12  func MustParseYAMLFromString(t *testing.T, s string) []K8sEntity {
    13  	entities, err := ParseYAMLFromString(s)
    14  	if err != nil {
    15  		t.Fatal(err)
    16  	}
    17  	return entities
    18  }
    19  
    20  func TestTracerYAML(t *testing.T) {
    21  	entities := MustParseYAMLFromString(t, testyaml.TracerYAML)
    22  	if len(entities) != 3 ||
    23  		entities[0].GVK().Kind != "Deployment" ||
    24  		entities[1].GVK().Kind != "Service" ||
    25  		entities[2].GVK().Kind != "Service" {
    26  		t.Errorf("Unexpected entities: %+v", entities)
    27  	}
    28  
    29  	result, err := SerializeSpecYAML(entities)
    30  	if err != nil {
    31  		t.Fatal(err)
    32  	}
    33  
    34  	if !strings.Contains(result, "image: openzipkin/zipkin") {
    35  		t.Errorf("image name did not appear in serialized yaml: %s", result)
    36  	}
    37  	if !strings.Contains(result, "name: tracer-prod") {
    38  		t.Errorf("service name did not appear in serialized yaml: %s", result)
    39  	}
    40  }
    41  
    42  func TestSanchoYAML(t *testing.T) {
    43  	entities := MustParseYAMLFromString(t, testyaml.SanchoYAML)
    44  	if len(entities) != 1 || entities[0].GVK().Kind != "Deployment" {
    45  		t.Errorf("Unexpected entities: %+v", entities)
    46  	}
    47  
    48  	result, err := SerializeSpecYAML(entities)
    49  	if err != nil {
    50  		t.Fatal(err)
    51  	}
    52  
    53  	if !strings.Contains(result, "image: gcr.io/some-project-162817/sancho") {
    54  		t.Errorf("image name did not appear in serialized yaml: %s", result)
    55  	}
    56  }
    57  
    58  func TestHelmGeneratedRedisYAML(t *testing.T) {
    59  	entities := MustParseYAMLFromString(t, testyaml.HelmGeneratedRedisYAML)
    60  	assert.Equal(t, 7, len(entities))
    61  
    62  	kinds := []string{}
    63  	for _, entity := range entities {
    64  		kinds = append(kinds, entity.GVK().Kind)
    65  	}
    66  	assert.Equal(t, []string{
    67  		"Secret",
    68  		"ConfigMap",
    69  		"ConfigMap",
    70  		"Service",
    71  		"Service",
    72  		"Deployment",
    73  		"StatefulSet",
    74  	}, kinds)
    75  }
    76  
    77  func TestCRDYAML(t *testing.T) {
    78  	entities := assertRoundTripYAML(t, testyaml.CRDYAML)
    79  	assert.Equal(t, 2, len(entities))
    80  
    81  	kinds := []string{}
    82  	names := []string{}
    83  	for _, entity := range entities {
    84  		kinds = append(kinds, entity.GVK().Kind)
    85  		names = append(names, entity.Name())
    86  	}
    87  	assert.Equal(t, []string{
    88  		"CustomResourceDefinition",
    89  		"Project",
    90  	}, kinds)
    91  	assert.Equal(t, []string{
    92  		"projects.example.martin-helmich.de",
    93  		"example-project",
    94  	}, names)
    95  }
    96  
    97  func TestPodDisruptionBudgetYAML(t *testing.T) {
    98  	// Old versions of Tilt would print the PodDisruptionBudgetStatus, which
    99  	// is not correct and leads to errors. See:
   100  	// https://github.com/tilt-dev/tilt/issues/1667
   101  	entities := assertRoundTripYAML(t, testyaml.PodDisruptionBudgetYAML)
   102  	assert.Equal(t, 1, len(entities))
   103  }
   104  
   105  func TestListYAML(t *testing.T) {
   106  	// We should unroll top-level lists
   107  	entities := MustParseYAMLFromString(t, testyaml.DoggosListYAML)
   108  	assert.Equal(t, 2, len(entities))
   109  
   110  	kinds := []string{}
   111  	for _, entity := range entities {
   112  		kinds = append(kinds, entity.GVK().Kind)
   113  	}
   114  	assert.Equal(t, []string{
   115  		"Service",
   116  		"Deployment",
   117  	}, kinds)
   118  }
   119  
   120  func TestBase64Serialization(t *testing.T) {
   121  	// json-iterator used to have a bug where it serialized this incorrectly.
   122  	// https://github.com/json-iterator/go/issues/272
   123  	yaml := `apiVersion: v1
   124  data:
   125    credentials: ""
   126  kind: Secret
   127  metadata:
   128    name: ldap
   129    namespace: myspace
   130  type: Opaque`
   131  	assertRoundTripYAML(t, yaml)
   132  }
   133  
   134  // Assert that parsing the YAML and re-serializing it produces the same result.
   135  // Returns the parsed entities.
   136  func assertRoundTripYAML(t *testing.T, yaml string) []K8sEntity {
   137  	entities := MustParseYAMLFromString(t, yaml)
   138  	result, err := SerializeSpecYAML(entities)
   139  	if err != nil {
   140  		t.Fatal(err)
   141  	}
   142  	expected := strings.TrimSpace(yaml)
   143  	result = strings.TrimSpace(result)
   144  	assert.Equal(t, expected, result)
   145  	return entities
   146  }