github.com/drone/go-convert@v0.0.0-20240307072510-6bd371c65e61/convert/github/yaml/container_test.go (about)

     1  package yaml
     2  
     3  import (
     4  	"github.com/google/go-cmp/cmp"
     5  	"gopkg.in/yaml.v3"
     6  	"testing"
     7  )
     8  
     9  func TestContainer(t *testing.T) {
    10  	tests := []struct {
    11  		yaml string
    12  		want Container
    13  	}{
    14  		// string value
    15  		{
    16  			yaml: `node:14.16`,
    17  			want: Container{Image: "node:14.16"},
    18  		},
    19  		// struct value
    20  		{
    21  			yaml: `
    22        image: node:14.16
    23        env:
    24          NODE_ENV: development
    25        ports:
    26          - 80
    27        volumes:
    28          - my_docker_volume:/volume_mount
    29        options: --cpus 1
    30  `,
    31  			want: Container{
    32  				Image: "node:14.16",
    33  				Env: map[string]string{
    34  					"NODE_ENV": "development",
    35  				},
    36  				Ports: []string{
    37  					"80",
    38  				},
    39  				Volumes: []string{
    40  					"my_docker_volume:/volume_mount",
    41  				},
    42  				Options: "--cpus 1",
    43  			},
    44  		},
    45  		// struct value
    46  		{
    47  			yaml: `
    48    image: ghcr.io/owner/image
    49    credentials:
    50       username: username
    51       password: password
    52  `,
    53  			want: Container{
    54  				Image: "ghcr.io/owner/image",
    55  				Credentials: &Credentials{
    56  					Username: "username",
    57  					Password: "password",
    58  				},
    59  			},
    60  		},
    61  	}
    62  
    63  	for i, test := range tests {
    64  		got := new(Container)
    65  		if err := yaml.Unmarshal([]byte(test.yaml), got); err != nil {
    66  			t.Log(test.yaml)
    67  			t.Error(err)
    68  			return
    69  		}
    70  		if diff := cmp.Diff(got, &test.want); diff != "" {
    71  			t.Log(test.yaml)
    72  			t.Errorf("Unexpected parsing results for test %v", i)
    73  			t.Log(diff)
    74  		}
    75  	}
    76  }
    77  
    78  func TestContainer_Error(t *testing.T) {
    79  	err := yaml.Unmarshal([]byte("[[]]"), new(Container))
    80  	if err == nil || err.Error() != "failed to unmarshal container" {
    81  		t.Errorf("Expect error, got %s", err)
    82  	}
    83  }