vitess.io/vitess@v0.16.2/examples/compose/vtcompose/vtcompose_test.go (about)

     1  /*
     2   * Copyright 2019 The Vitess Authors.
     3  
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7  
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9  
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  package main
    18  
    19  import (
    20  	"fmt"
    21  	"strconv"
    22  	"testing"
    23  
    24  	"github.com/stretchr/testify/assert"
    25  )
    26  
    27  var (
    28  	referenceYaml         = string(readFile("./docker-compose.test.yml"))
    29  	testComposeFile       = readFile("./docker-compose.base.yml")
    30  	testKeyspaceInfoMap   = parseKeyspaceInfo(DefaultKeyspaceData)
    31  	testExternalDbInfoMap = parseExternalDbData(DefaultExternalDbData)
    32  
    33  	testVtOpts = vtOptions{
    34  		webPort:       DefaultWebPort,
    35  		gRpcPort:      DefaultGrpcPort,
    36  		mySqlPort:     DefaultMysqlPort,
    37  		topologyFlags: DefaultTopologyFlags,
    38  		cell:          DefaultCell,
    39  	}
    40  )
    41  
    42  func TestGenerateCorrectFileWithDefaultOpts(t *testing.T) {
    43  	baseFile := testComposeFile
    44  	finalFile := applyDockerComposePatches(baseFile, testKeyspaceInfoMap, testExternalDbInfoMap, testVtOpts)
    45  
    46  	yamlString := string(finalFile)
    47  	assert.YAMLEq(t, referenceYaml, yamlString)
    48  }
    49  
    50  func TestOptsAppliedThroughoutGeneratedFile(t *testing.T) {
    51  	baseFile := testComposeFile
    52  	options := vtOptions{
    53  		webPort:       55_555,
    54  		gRpcPort:      66_666,
    55  		mySqlPort:     77_777,
    56  		topologyFlags: "-custom -flags",
    57  		cell:          "custom cell",
    58  	}
    59  	finalFile := applyDockerComposePatches(baseFile, testKeyspaceInfoMap, testExternalDbInfoMap, options)
    60  	yamlString := string(finalFile)
    61  
    62  	// These asserts are not exhaustive, but should cover most cases.
    63  	assert.NotContains(t, yamlString, strconv.Itoa(DefaultWebPort))
    64  	assert.Contains(t, yamlString, strconv.Itoa(options.webPort))
    65  
    66  	assert.NotContains(t, yamlString, strconv.Itoa(DefaultGrpcPort))
    67  	assert.Contains(t, yamlString, strconv.Itoa(options.gRpcPort))
    68  
    69  	assert.NotContains(t, yamlString, ":"+strconv.Itoa(DefaultMysqlPort))
    70  	assert.Contains(t, yamlString, ":"+strconv.Itoa(options.webPort))
    71  
    72  	assert.NotContains(t, yamlString, fmt.Sprintf("-cell %s", DefaultCell))
    73  	assert.Contains(t, yamlString, fmt.Sprintf("-cell %s", options.cell))
    74  
    75  	assert.Contains(t, yamlString, fmt.Sprintf("- TOPOLOGY_FLAGS=%s", options.topologyFlags))
    76  	assert.NotContains(t, yamlString, DefaultTopologyFlags)
    77  }