something is going on

This commit is contained in:
Nikolai Rodionov
2023-11-05 13:03:35 +01:00
parent 7b327b38e7
commit d469a758f5
17 changed files with 249 additions and 35 deletions

View File

@ -0,0 +1,50 @@
// It's called bundle to avoid a conflict with the GO package
package bundle
import (
"fmt"
"os"
"path/filepath"
"git.badhouseplants.net/allanger/shoebill/pkg/types/metadata"
"git.badhouseplants.net/allanger/shoebill/pkg/types/workload"
"github.com/sirupsen/logrus"
"gopkg.in/yaml.v2"
)
type Bundle struct {
// A bundle's metadata
Metadata *metadata.Metadata `yaml:"metadata"`
BundlePath string `yaml:"-"`
Workload *workload.Workload `yaml:"-"`
}
type Image struct {
Repository string
Tag string
}
// Init a new bundle struct from the file
func ReadFromFile(filePath string) (*Bundle, error) {
var bundle Bundle
logrus.Infof("readig the bundle file: %s", filePath)
bundleFile, err := os.ReadFile(filePath)
if err != nil {
return nil, err
}
if err := yaml.Unmarshal(bundleFile, &bundle); err != nil {
return nil, err
}
bundle.BundlePath = filepath.Dir(filePath)
return &bundle, nil
}
func (bundle *Bundle) Bootstrap() (err error) {
// TODO: It's ugly as hell
workloadFilePath := fmt.Sprintf("%s/bundle/workload.yaml", bundle.BundlePath)
bundle.Workload, err = workload.ReadFromFile(workloadFilePath)
if err != nil {
return
}
return nil
}

View File

@ -0,0 +1,62 @@
package bundle_test
import (
"os"
"testing"
"git.badhouseplants.net/allanger/shoebill/pkg/types/bundle"
"git.badhouseplants.net/allanger/shoebill/utils/helpers"
"github.com/stretchr/testify/assert"
)
// Test that the file can be read
func TestUnitReadValidFile(t *testing.T) {
f := helpers.CreateFile(t)
defer os.Remove(f.Name())
const validFile = `---
metadata:
name: db-operator
version: 1.0.0
maintainer:
name: allanger
email: allanger@badhouseplants.net
website: https://badhouseplants.net
`
helpers.FillFile(t, f, validFile)
bundle, err := bundle.ReadFromFile(f.Name())
// Check that there is no unexpected error
assert.NoError(t, err)
// Check the bundle metadata
assert.Equal(t, "1.0.0", bundle.Metadata.Version)
assert.Equal(t, "db-operator", bundle.Metadata.Name)
assert.Equal(t, "allanger", bundle.Metadata.Maintainer.Name)
assert.Equal(t, "allanger@badhouseplants.net", bundle.Metadata.Maintainer.Email)
assert.Equal(t, "https://badhouseplants.net", bundle.Metadata.Maintainer.Website)
}
func TestIntegrationBootstrap(t *testing.T) {
bundlePath := "../../../test/test-bundle/bundle.yaml"
bundle, err := bundle.ReadFromFile(bundlePath)
// Check that there is no unexpected error
assert.NoError(t, err)
// Check the bundle file in the test folder to get the actual data
assert.Equal(t, "0.0.1", bundle.Metadata.Version)
assert.Equal(t, "test-bundle", bundle.Metadata.Name)
assert.Equal(t, "allanger", bundle.Metadata.Maintainer.Name)
assert.Equal(t, "allanger@badhouseplants.net", bundle.Metadata.Maintainer.Email)
assert.Equal(t, "https://badhouseplants.net", bundle.Metadata.Maintainer.Website)
err = bundle.Bootstrap()
assert.NoError(t, err)
assert.Equal(t, "Deployment", bundle.Workload.Kind)
assert.Equal(t, 1, bundle.Workload.Replicas)
}