Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 7 additions & 13 deletions internal/command/generate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,25 +93,19 @@ func TestInitAndGenerate_with_sample(t *testing.T) {
})
require.NoError(t, err)
assert.Equal(t, "", stdout)
raw, err := os.ReadFile(filepath.Join(td, "values.yaml"))

// verify the output file was created
_, err = os.Stat(filepath.Join(td, "values.yaml"))
assert.NoError(t, err)
assert.Equal(t, `containers:
main:
image:
name: stefanprodan/podinfo
service:
ports:
- name: web
port: 8080
`, string(raw))

// check that state was persisted
// check that state was persisted with the new sample structure
sd, ok, err := state.LoadStateDirectory(td)
assert.NoError(t, err)
assert.True(t, ok)
assert.Equal(t, "score.yaml", *sd.State.Workloads["example"].File)
assert.Equal(t, "score.yaml", *sd.State.Workloads["hello-world"].File)
assert.Len(t, sd.State.Workloads, 1)
assert.Len(t, sd.State.Resources, 0)
// new sample declares 3 resources: postgres, dns, and route
assert.Len(t, sd.State.Resources, 3)
}

func TestInitAndGenerate_with_full_example(t *testing.T) {
Expand Down
61 changes: 32 additions & 29 deletions internal/command/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,51 @@
package command

import (
"bytes"
"errors"
"fmt"
"log/slog"
"os"

"github.com/score-spec/score-go/framework"
scoretypes "github.com/score-spec/score-go/types"
"github.com/spf13/cobra"
"gopkg.in/yaml.v3"

"github.com/score-spec/score-helm/internal/state"
)

const (
initCmdFileFlag = "file"
initCmdFileNoSampleFlag = "no-sample"

DefaultScoreFileContent = `# Score provides a developer-centric and platform-agnostic
# Workload specification to improve developer productivity and experience.
# Score eliminates configuration management between local and remote environments.
#
# Get started with Score: https://docs.score.dev/docs/get-started/.
---
apiVersion: score.dev/v1b1
metadata:
name: hello-world
annotations:
tags: "nodejs,http,website,javascript,postgres"
containers:
hello-world:
image: scorespec/sample-score-app:latest
variables:
PORT: "3000"
MESSAGE: "Hello, World!"
resources:
db:
type: postgres
dns:
type: dns
route:
type: route
service:
ports:
www:
port: 8080
targetPort: 3000
`
)

var initCmd = &cobra.Command{
Expand Down Expand Up @@ -74,32 +102,7 @@ var initCmd = &cobra.Command{
if !errors.Is(err, os.ErrNotExist) {
return fmt.Errorf("failed to check for existing Score file: %w", err)
}
workload := &scoretypes.Workload{
ApiVersion: "score.dev/v1b1",
Metadata: map[string]interface{}{
"name": "example",
},
Containers: map[string]scoretypes.Container{
"main": {
Image: "stefanprodan/podinfo",
},
},
Service: &scoretypes.WorkloadService{
Ports: map[string]scoretypes.ServicePort{
"web": {Port: 8080},
},
},
}
var rawScore bytes.Buffer
scoreEnc := yaml.NewEncoder(&rawScore)
scoreEnc.SetIndent(2)
if err := scoreEnc.Encode(workload); err != nil {
return fmt.Errorf("failed to encode Score file: %w", err)
}
if err := scoreEnc.Close(); err != nil {
return fmt.Errorf("failed to encode Score file: %w", err)
}
if err := os.WriteFile(initCmdScoreFile, rawScore.Bytes(), 0755); err != nil {
if err := os.WriteFile(initCmdScoreFile, []byte(DefaultScoreFileContent), 0755); err != nil {
return fmt.Errorf("failed to write Score file: %w", err)
}
slog.Info("Created initial Score file", "file", initCmdScoreFile)
Expand Down
19 changes: 7 additions & 12 deletions internal/command/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,8 @@ func TestInitNominal(t *testing.T) {

raw, err := os.ReadFile("score.yaml")
require.NoError(t, err)
assert.Equal(t, `apiVersion: score.dev/v1b1
containers:
main:
image: stefanprodan/podinfo
metadata:
name: example
service:
ports:
web:
port: 8080
`, string(raw))
assert.Contains(t, string(raw), "name: hello-world")
assert.Contains(t, string(raw), "scorespec/sample-score-app:latest")

stdout, stderr, err = executeAndResetCommand(context.Background(), rootCmd, []string{"generate", "score.yaml"})
assert.NoError(t, err)
Expand All @@ -65,7 +56,11 @@ service:
if assert.True(t, ok) {
assert.Equal(t, state.DefaultRelativeStateDirectory, sd.Path)
assert.Len(t, sd.State.Workloads, 1)
assert.Equal(t, map[framework.ResourceUid]framework.ScoreResourceState[state.ResourceExtras]{}, sd.State.Resources)
assert.Contains(t, sd.State.Workloads, "hello-world")
assert.Len(t, sd.State.Resources, 3)
assert.Contains(t, sd.State.Resources, framework.ResourceUid("postgres.default#hello-world.db"))
assert.Contains(t, sd.State.Resources, framework.ResourceUid("dns.default#hello-world.dns"))
assert.Contains(t, sd.State.Resources, framework.ResourceUid("route.default#hello-world.route"))
assert.Equal(t, map[string]interface{}{}, sd.State.SharedState)
}
}
Expand Down