Skip to content
Snippets Groups Projects
Commit f6fc32a1 authored by Jessica Yuen's avatar Jessica Yuen
Browse files

Rename <env>.jsonnet file to main.jsonnet

Currently, creating a `dev` environment will create a file
`dev.jsonnet`. Creating a `prod` environment will create a file
`prod.jsonnet`. This is a little more complex and prone to error than it
needs to be, especially when we are renaming environments. It will also
make this file easier to refer to in documentation if we give the file a
static name -- `main.jsonnet`.
parent 527db11b
No related branches found
No related tags found
No related merge requests found
......@@ -36,11 +36,15 @@ const (
defaultEnvName = "default"
metadataDirName = ".metadata"
// hidden metadata files
schemaFilename = "swagger.json"
extensionsLibFilename = "k.libsonnet"
k8sLibFilename = "k8s.libsonnet"
paramsFileName = "params.libsonnet"
specFilename = "spec.json"
// primary environment files
envFileName = "main.jsonnet"
paramsFileName = "params.libsonnet"
specFilename = "spec.json"
)
// Environment represents all fields of a ksonnet environment
......@@ -129,7 +133,7 @@ func (m *manager) createEnvironment(name, server, namespace string, extensionsLi
},
{
// environment base override file
appendToAbsPath(envPath, path.Base(name)+".jsonnet"),
appendToAbsPath(envPath, envFileName),
m.generateOverrideData(),
},
{
......@@ -281,10 +285,9 @@ func (m *manager) SetEnvironment(name string, desired *Environment) error {
//
// Move the directory
//
pathOld := appendToAbsPath(m.environmentsPath, name)
pathNew := appendToAbsPath(m.environmentsPath, desired.Name)
jsonnetPathOld := string(appendToAbsPath(pathOld, path.Base(name)+".jsonnet"))
jsonnetPathNew := string(appendToAbsPath(pathOld, path.Base(desired.Name)+".jsonnet"))
exists, err := afero.DirExists(m.appFS, string(pathNew))
if err != nil {
return err
......@@ -299,13 +302,6 @@ func (m *manager) SetEnvironment(name string, desired *Environment) error {
if err != nil {
return err
}
// note: we have to move the jsonnet file first because of a bug with afero: spf13/afero:#141
log.Debugf("Renaming jsonnet file from '%s' to '%s'", path.Base(jsonnetPathOld), path.Base(jsonnetPathNew))
err = m.appFS.Rename(jsonnetPathOld, jsonnetPathNew)
if err != nil {
log.Debugf("Failed to move path '%s' to '%s", jsonnetPathOld, jsonnetPathNew)
return err
}
// finally, move the directory
err = m.appFS.Rename(string(pathOld), string(pathNew))
if err != nil {
......
......@@ -18,7 +18,6 @@ package metadata
import (
"encoding/json"
"fmt"
"path"
"reflect"
"strings"
"testing"
......@@ -28,7 +27,6 @@ import (
)
const (
mockSpecJSON = "spec.json"
mockSpecJSONServer = "localhost:8080"
mockEnvName = "us-west/test"
......@@ -58,15 +56,15 @@ func mockEnvironments(t *testing.T, appName string) *manager {
testFS.Mkdir(string(envPath), defaultFolderPermissions)
testDirExists(t, string(envPath))
jsonnetPath := appendToAbsPath(envPath, path.Base(env)+".jsonnet")
jsonnetData := m.generateOverrideData()
err = afero.WriteFile(testFS, string(jsonnetPath), jsonnetData, defaultFilePermissions)
envFilePath := appendToAbsPath(envPath, envFileName)
envFileData := m.generateOverrideData()
err = afero.WriteFile(testFS, string(envFilePath), envFileData, defaultFilePermissions)
if err != nil {
t.Fatalf("Could not write file at path: %s", jsonnetPath)
t.Fatalf("Could not write file at path: %s", envFilePath)
}
testFileExists(t, string(jsonnetPath))
testFileExists(t, string(envFilePath))
specPath := appendToAbsPath(envPath, mockSpecJSON)
specPath := appendToAbsPath(envPath, specFilename)
specData, err := generateSpecData(mockSpecJSONServer, mockNamespace)
if err != nil {
t.Fatalf("Expected to marshal:\nserver: %s\nnamespace: %s\n, but failed", mockSpecJSONServer, mockNamespace)
......@@ -75,6 +73,15 @@ func mockEnvironments(t *testing.T, appName string) *manager {
if err != nil {
t.Fatalf("Could not write file at path: %s", specPath)
}
testFileExists(t, string(specPath))
paramsPath := appendToAbsPath(envPath, paramsFileName)
paramsData := m.generateParamsData()
err = afero.WriteFile(testFS, string(paramsPath), paramsData, defaultFilePermissions)
if err != nil {
t.Fatalf("Could not write file at path: %s", paramsPath)
}
testFileExists(t, string(paramsPath))
}
return m
......@@ -172,26 +179,42 @@ func TestSetEnvironment(t *testing.T) {
t.Fatalf("Expected error when setting \"%s\" to \"%s\", because env already exists", mockEnvName, mockEnvName2)
}
//
// Test changing the name and server of a an existing environment.
// Ensure new env directory is created, and old directory no longer exists.
// Also ensure server is set in spec.json
//
err = m.SetEnvironment(mockEnvName, &set)
if err != nil {
t.Fatalf("Could not set \"%s\", got:\n %s", mockEnvName, err)
}
// Ensure new env directory is created, and old directory no longer exists.
envPath := appendToAbsPath(AbsPath(appName), environmentsDir)
expectedPathExists := appendToAbsPath(envPath, set.Name)
expectedPathNotExists := appendToAbsPath(envPath, mockEnvName)
testDirExists(t, string(expectedPathExists))
testDirNotExists(t, string(expectedPathNotExists))
expectedSpecPath := appendToAbsPath(expectedPathExists, specFilename)
specData, err := afero.ReadFile(testFS, string(expectedSpecPath))
// BUG: https://github.com/spf13/afero/issues/141
// we aren't able to test this until the above is fixed.
//
// ensure all files are moved
//
// expectedFiles := []string{
// envFileName,
// specFilename,
// paramsFileName,
// }
// for _, f := range expectedFiles {
// expectedFilePath := appendToAbsPath(expectedPathExists, f)
// testFileExists(t, string(expectedFilePath))
// }
// ensure spec file contains the correct content
specData, err := afero.ReadFile(testFS, string(appendToAbsPath(expectedPathExists, specFilename)))
if err != nil {
t.Fatalf("Failed to read spec file:\n %s", err)
}
var envSpec EnvironmentSpec
err = json.Unmarshal(specData, &envSpec)
if err != nil {
......
......@@ -255,7 +255,7 @@ func (m *manager) CreateComponent(name string, text string, params param.Params,
func (m *manager) LibPaths(envName string) (libPath, vendorPath, envLibPath, envComponentPath, envParamsPath AbsPath) {
envPath := appendToAbsPath(m.environmentsPath, envName)
return m.libPath, m.vendorPath, appendToAbsPath(envPath, metadataDirName),
appendToAbsPath(envPath, path.Base(envName)+".jsonnet"), appendToAbsPath(envPath, componentParamsFile)
appendToAbsPath(envPath, envFileName), appendToAbsPath(envPath, componentParamsFile)
}
func (m *manager) GetComponentParams(component string) (param.Params, error) {
......
......@@ -266,7 +266,7 @@ func TestLibPaths(t *testing.T) {
expectedVendorPath := path.Join(appName, vendorDir)
expectedLibPath := path.Join(appName, libDir)
expectedEnvLibPath := path.Join(appName, environmentsDir, mockEnvName, metadataDirName)
expectedEnvComponentPath := path.Join(appName, environmentsDir, mockEnvName, path.Base(mockEnvName)+".jsonnet")
expectedEnvComponentPath := path.Join(appName, environmentsDir, mockEnvName, envFileName)
expectedEnvParamsPath := path.Join(appName, environmentsDir, mockEnvName, paramsFileName)
m := mockEnvironments(t, appName)
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment