github.com/secure-build/gitlab-runner@v12.5.0+incompatible/helpers/gitlab_ci_yaml_parser/data_bag_test.go (about)

     1  package gitlab_ci_yaml_parser
     2  
     3  import (
     4  	"encoding/json"
     5  	"github.com/stretchr/testify/assert"
     6  	"github.com/stretchr/testify/require"
     7  	"gopkg.in/yaml.v2"
     8  	"testing"
     9  )
    10  
    11  type dataOptions struct {
    12  	String  string `json:"string"`
    13  	Integer int    `json:"integer"`
    14  }
    15  
    16  type testOptions struct {
    17  	Root string       `json:"root"`
    18  	Data *dataOptions `json:"data"`
    19  }
    20  
    21  type buildTest struct {
    22  	DataBag `json:"options"`
    23  }
    24  
    25  const exampleOptionsJSON = `{
    26  	"options": {
    27  		"root": "value",
    28  		"data": {
    29  			"string": "value",
    30  			"integer": 1
    31  		}
    32  	}
    33  }`
    34  
    35  const exampleOptionsNoDataJSON = `{
    36  	"options": {
    37  		"root": "value"
    38  	}
    39  }`
    40  
    41  const exampleOptionsYAML = `
    42  image: test:latest
    43  
    44  variables:
    45      KEY: value
    46  `
    47  
    48  func (o *buildTest) Unmarshal(data string) error {
    49  	return json.Unmarshal([]byte(data), o)
    50  }
    51  
    52  func TestDataBagUnmarshaling(t *testing.T) {
    53  	var options buildTest
    54  	require.NoError(t, options.Unmarshal(exampleOptionsJSON))
    55  	assert.Equal(t, "value", options.DataBag["root"])
    56  
    57  	result, _ := options.Get("data", "string")
    58  	assert.Equal(t, "value", result)
    59  	result, _ = options.Get("data", "integer")
    60  	assert.Equal(t, float64(1), result)
    61  
    62  	result2, _ := options.GetString("data", "string")
    63  	assert.Equal(t, "value", result2)
    64  	result2, _ = options.GetString("data", "integer")
    65  	assert.Equal(t, "", result2)
    66  }
    67  
    68  func TestDataBagDecodeTest(t *testing.T) {
    69  	var options buildTest
    70  	var test testOptions
    71  	require.NoError(t, options.Unmarshal(exampleOptionsJSON))
    72  	require.NoError(t, options.Decode(&test))
    73  	assert.Equal(t, "value", test.Root)
    74  	assert.NotNil(t, test.Data)
    75  }
    76  
    77  func TestDataBagDecodeTestNoData(t *testing.T) {
    78  	var options buildTest
    79  	var test testOptions
    80  	require.NoError(t, options.Unmarshal(exampleOptionsNoDataJSON))
    81  	require.NoError(t, options.Decode(&test))
    82  	assert.Equal(t, "value", test.Root)
    83  	assert.Nil(t, test.Data)
    84  }
    85  
    86  func TestDataBagDecodeData(t *testing.T) {
    87  	var options buildTest
    88  	var data dataOptions
    89  	require.NoError(t, options.Unmarshal(exampleOptionsJSON))
    90  	require.NoError(t, options.Decode(&data, "data"))
    91  	assert.Equal(t, "value", data.String)
    92  	assert.Equal(t, 1, data.Integer)
    93  }
    94  
    95  func TestDataBagSanitizeWithYamlDecode(t *testing.T) {
    96  	options := make(DataBag)
    97  
    98  	require.NoError(t, yaml.Unmarshal([]byte(exampleOptionsYAML), options))
    99  	assert.Equal(t, DataBag{
   100  		"image": "test:latest",
   101  		"variables": map[interface{}]interface{}{
   102  			"KEY": "value",
   103  		},
   104  	}, options)
   105  
   106  	require.NoError(t, options.Sanitize())
   107  	assert.Equal(t, DataBag{
   108  		"image": "test:latest",
   109  		"variables": map[string]interface{}{
   110  			"KEY": "value",
   111  		},
   112  	}, options)
   113  }