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

     1  // Copyright 2022 Harness, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package yaml
    16  
    17  import (
    18  	"testing"
    19  
    20  	"github.com/google/go-cmp/cmp"
    21  	"gopkg.in/yaml.v3"
    22  )
    23  
    24  func TestEnvironment(t *testing.T) {
    25  	tests := []struct {
    26  		yaml string
    27  		want Environment
    28  	}{
    29  		{
    30  			yaml: `"production"`,
    31  			want: Environment{
    32  				Name: "production",
    33  			},
    34  		},
    35  		{
    36  			yaml: `{ "name": "production", "url": "https://prod.example.com" }`,
    37  			want: Environment{
    38  				Name: "production",
    39  				Url:  "https://prod.example.com",
    40  			},
    41  		},
    42  		{
    43  			yaml: `{ "name": "production", "url": "https://prod.example.com", "kubernetes": { "namespace": "production" } }`,
    44  			want: Environment{
    45  				Name: "production",
    46  				Url:  "https://prod.example.com",
    47  				Kubernetes: &Kubernetes{
    48  					Namespace: "production",
    49  				},
    50  			},
    51  		},
    52  	}
    53  
    54  	for i, test := range tests {
    55  		got := new(Environment)
    56  		if err := yaml.Unmarshal([]byte(test.yaml), got); err != nil {
    57  			t.Error(err)
    58  			return
    59  		}
    60  		if diff := cmp.Diff(got, &test.want); diff != "" {
    61  			t.Errorf("Unexpected parsing results for test %v", i)
    62  			t.Log(diff)
    63  		}
    64  	}
    65  }
    66  
    67  func TestEnviron_Error(t *testing.T) {
    68  	err := yaml.Unmarshal([]byte("[]"), new(Environment))
    69  	if err == nil || err.Error() != "failed to unmarshal environment" {
    70  		t.Errorf("Expect error, got %s", err)
    71  	}
    72  }
    73  
    74  // deploy to production:
    75  //   stage: deploy
    76  //   script: git push production HEAD:main
    77  //   environment: production
    78  
    79  // deploy to production:
    80  //   stage: deploy
    81  //   script: git push production HEAD:main
    82  //   environment:
    83  //     name: production
    84  
    85  // deploy to production:
    86  //   stage: deploy
    87  //   script: git push production HEAD:main
    88  //   environment:
    89  //     name: production
    90  //     url: https://prod.example.com
    91  
    92  // stop_review_app:
    93  //   stage: deploy
    94  //   variables:
    95  //     GIT_STRATEGY: none
    96  //   script: make delete-app
    97  //   when: manual
    98  //   environment:
    99  //     name: review/$CI_COMMIT_REF_SLUG
   100  //     action: stop
   101  
   102  // review_app:
   103  //   script: deploy-review-app
   104  //   environment:
   105  //     name: review/$CI_COMMIT_REF_SLUG
   106  //     auto_stop_in: 1 day
   107  
   108  // deploy:
   109  //   stage: deploy
   110  //   script: make deploy-app
   111  //   environment:
   112  //     name: production
   113  //     kubernetes:
   114  //       namespace: production
   115  
   116  // deploy:
   117  //   script: echo
   118  //   environment:
   119  //     name: customer-portal
   120  //     deployment_tier: production