sg/msp: improve test coverage on examples, fix project ID generation (#59343)

This commit is contained in:
Robert Lin 2024-01-04 16:13:11 -08:00 committed by GitHub
parent 3d9f69df4f
commit 4ca528adc0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
14 changed files with 456 additions and 132 deletions

View File

@ -98,7 +98,7 @@ func (s EnvironmentSpec) Validate() []error {
errs = append(errs, errors.New("projectID is required"))
}
if len(s.ProjectID) > 30 {
errs = append(errs, errors.New("projectID must be less than 30 characters"))
errs = append(errs, errors.Newf("projectID %q must be less than 30 characters", s.ProjectID))
}
if !strings.Contains(s.ProjectID, fmt.Sprintf("-%s-", s.ID)) {
errs = append(errs, errors.Newf("projectID %q must contain environment ID: expecting format '$SERVICE_ID-$ENVIRONMENT_ID-$RANDOM_SUFFIX'",

View File

@ -3,6 +3,7 @@ package example
import (
"errors"
"fmt"
"strings"
"testing"
"github.com/hexops/autogold/v2"
@ -24,7 +25,7 @@ func mockNewProjectID(t *testing.T) {
if l == 0 {
return "", errors.New("expected length > 0")
}
return fmt.Sprintf("%s-%s-%s", s, e, t.Name()), nil
return fmt.Sprintf("%s-%s-%s", s, e, strings.Repeat("x", l)), nil
}
t.Cleanup(func() { templateFuncs[newProjectIDFuncKey] = spec.NewProjectID })
}
@ -32,128 +33,82 @@ func mockNewProjectID(t *testing.T) {
func TestNewService(t *testing.T) {
mockNewProjectID(t)
f, err := NewService(Template{
ID: "msp-example",
Dev: true,
Owner: "core-services",
for _, tc := range []struct {
name string
template Template
}{
{
name: "dev",
template: Template{
ID: "msp-example",
Dev: true,
Owner: "core-services",
ProjectIDSuffixLength: 4,
})
require.NoError(t, err)
ProjectIDSuffixLength: 4,
},
},
{
name: "prod",
template: Template{
ID: "msp-example",
Dev: false,
Owner: "core-services",
autogold.Expect(`service:
id: msp-example
name: Msp Example
owners:
- core-services
build:
# TODO: Configure the correct image for your service here. If you use a private
# registry like us.gcr.io or Artifact Registry, access will automatically be
# granted for your service to pull the correct image.
image: us.gcr.io/sourcegraph-dev/msp-example
# TODO: Configure where the source code for your service lives here.
source:
repo: github.com/sourcegraph/sourcegraph
dir: cmd/msp-example
environments:
- id: dev
projectID: msp-example-dev-TestNewService
# TODO: We initially provision in 'test' to make it easy to access the project
# during setup. Once done, you should change this to 'external' or 'internal'.
category: test
# Specify a deployment strategy for upgrades.
deploy:
type: manual
manual:
tag: insiders
# Specify an externally facing domain.
domain:
type: cloudflare
cloudflare:
subdomain: msp-example
zone: sgdev.org
# Specify environment configuration your service needs to operate.
env:
SRC_LOG_LEVEL: info
SRC_LOG_FORMAT: json_gcp
# Specify how your service should scale.
instances:
resources:
cpu: 1
memory: 1Gi
scaling:
maxCount: 3
minCount: 1
startupProbe:
# Only enable if your service implements MSP /-/healthz conventions.
disabled: true
`).Equal(t, string(f))
t.Run("is valid", func(t *testing.T) {
var s spec.Spec
require.NoError(t, yaml.Unmarshal(f, &s))
assert.Empty(t, s.Validate())
})
testInsertProdEnvironment(t, "msp-example", f)
ProjectIDSuffixLength: 4,
},
},
} {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
testRender(t, NewService, tc.template)
})
}
}
func TestNewJob(t *testing.T) {
mockNewProjectID(t)
f, err := NewJob(Template{
ID: "msp-example",
Dev: true,
Owner: "core-services",
for _, tc := range []struct {
name string
template Template
}{
{
name: "dev",
template: Template{
ID: "msp-example",
Dev: true,
Owner: "core-services",
ProjectIDSuffixLength: 4,
})
ProjectIDSuffixLength: 4,
},
},
{
name: "prod",
template: Template{
ID: "msp-example",
Dev: false,
Owner: "core-services",
ProjectIDSuffixLength: 4,
},
},
} {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
testRender(t, NewJob, tc.template)
})
}
}
func testRender(t *testing.T, renderFn func(t Template) ([]byte, error), template Template) {
f, err := renderFn(template)
require.NoError(t, err)
autogold.Expect(`service:
kind: job
id: msp-example
name: Msp Example
owners:
- core-services
build:
# TODO: Configure the correct image for your job here. If you use a private
# registry like us.gcr.io or Artifact Registry, access will automatically be
# granted for your job to pull the correct image.
image: us.gcr.io/sourcegraph-dev/msp-example
# TODO: Configure where the source code for your job lives here.
source:
repo: github.com/sourcegraph/sourcegraph
dir: cmd/msp-example
environments:
- id: dev
projectID: msp-example-dev-TestNewJob
# TODO: We initially provision in 'test' to make it easy to access the project
# during setup. Once done, you should change this to 'external' or 'internal'.
category: test
# Specify a strategy for updating the image.
deploy:
type: manual
manual:
tag: insiders
# Specify the schedule at which to run your job.
schedule:
cron: 0 * * * *
deadline: 600 # 10 minutes
# Specify environment configuration your service needs to operate.
env:
SRC_LOG_LEVEL: info
SRC_LOG_FORMAT: json_gcp
# Specify the resources your job gets.
instances:
resources:
cpu: 1
memory: 1Gi
`).Equal(t, string(f))
t.Run("spec", func(t *testing.T) {
autogold.ExpectFile(t, autogold.Raw(string(f)))
})
t.Run("is valid", func(t *testing.T) {
var s spec.Spec
@ -161,19 +116,15 @@ environments:
assert.Empty(t, s.Validate())
})
testInsertProdEnvironment(t, "msp-example", f)
}
func testInsertProdEnvironment(t *testing.T, serviceID string, specData []byte) {
t.Run("testInsertProdEnvironment", func(t *testing.T) {
t.Run("insert environment", func(t *testing.T) {
e, err := NewEnvironment(EnvironmentTemplate{
ServiceID: serviceID,
EnvironmentID: "prod",
ProjectIDSuffixLength: 4,
ServiceID: template.ID,
EnvironmentID: "second",
ProjectIDSuffixLength: template.ProjectIDSuffixLength,
})
require.NoError(t, err)
updatedSpecData, err := spec.AppendEnvironment(specData, e)
updatedSpecData, err := spec.AppendEnvironment(f, e)
require.NoError(t, err)
autogold.ExpectFile(t, autogold.Raw(string(updatedSpecData)))

View File

@ -17,7 +17,7 @@ build:
environments:
- id: {{ if .Dev }}dev{{ else }}prod{{ end }}
projectID: {{ if .Dev }}{{ newProjectID .ID "dev" .ProjectIDSuffixLength }}{{ else }}{{ newProjectID .ID "dev" .ProjectIDSuffixLength }}{{ end }}
projectID: {{ if .Dev }}{{ newProjectID .ID "dev" .ProjectIDSuffixLength }}{{ else }}{{ newProjectID .ID "prod" .ProjectIDSuffixLength }}{{ end }}
# TODO: We initially provision in 'test' to make it easy to access the project
# during setup. Once done, you should change this to 'external' or 'internal'.
category: test

View File

@ -16,7 +16,7 @@ build:
environments:
- id: {{ if .Dev }}dev{{ else }}prod{{ end }}
projectID: {{ if .Dev }}{{ newProjectID .ID "dev" .ProjectIDSuffixLength }}{{ else }}{{ newProjectID .ID "dev" .ProjectIDSuffixLength }}{{ end }}
projectID: {{ if .Dev }}{{ newProjectID .ID "dev" .ProjectIDSuffixLength }}{{ else }}{{ newProjectID .ID "prod" .ProjectIDSuffixLength }}{{ end }}
# TODO: We initially provision in 'test' to make it easy to access the project
# during setup. Once done, you should change this to 'external' or 'internal'.
category: test

View File

@ -0,0 +1,46 @@
service:
kind: job
id: msp-example
name: Msp Example
owners:
- core-services
build:
# TODO: Configure the correct image for your job here. If you use a private
# registry like us.gcr.io or Artifact Registry, access will automatically be
# granted for your job to pull the correct image.
image: us.gcr.io/sourcegraph-dev/msp-example
# TODO: Configure where the source code for your job lives here.
source:
repo: github.com/sourcegraph/sourcegraph
dir: cmd/msp-example
environments:
- id: dev
projectID: msp-example-dev-xxxx
# TODO: We initially provision in 'test' to make it easy to access the project
# during setup. Once done, you should change this to 'external' or 'internal'.
category: test
# Specify a strategy for updating the image.
deploy:
type: manual
manual:
tag: insiders
# Specify the schedule at which to run your job.
schedule:
cron: 0 * * * *
deadline: 600 # 10 minutes
# Specify environment configuration your service needs to operate.
env:
SRC_LOG_LEVEL: info
SRC_LOG_FORMAT: json_gcp
# Specify the resources your job gets.
instances:
resources:
cpu: 1
memory: 1Gi
- id: second
projectID: msp-example-second-xxxx
# TODO: We initially provision in 'test' to make it easy to access the project
# during setup. Once done, you should change this to 'external' or 'internal'.
category: test
# TODO: Fill out the rest of your configuration here!
# ...

View File

@ -0,0 +1,41 @@
service:
kind: job
id: msp-example
name: Msp Example
owners:
- core-services
build:
# TODO: Configure the correct image for your job here. If you use a private
# registry like us.gcr.io or Artifact Registry, access will automatically be
# granted for your job to pull the correct image.
image: us.gcr.io/sourcegraph-dev/msp-example
# TODO: Configure where the source code for your job lives here.
source:
repo: github.com/sourcegraph/sourcegraph
dir: cmd/msp-example
environments:
- id: dev
projectID: msp-example-dev-xxxx
# TODO: We initially provision in 'test' to make it easy to access the project
# during setup. Once done, you should change this to 'external' or 'internal'.
category: test
# Specify a strategy for updating the image.
deploy:
type: manual
manual:
tag: insiders
# Specify the schedule at which to run your job.
schedule:
cron: 0 * * * *
deadline: 600 # 10 minutes
# Specify environment configuration your service needs to operate.
env:
SRC_LOG_LEVEL: info
SRC_LOG_FORMAT: json_gcp
# Specify the resources your job gets.
instances:
resources:
cpu: 1
memory: 1Gi

View File

@ -14,8 +14,8 @@ build:
repo: github.com/sourcegraph/sourcegraph
dir: cmd/msp-example
environments:
- id: dev
projectID: msp-example-dev-TestNewJob
- id: prod
projectID: msp-example-prod-xxxx
# TODO: We initially provision in 'test' to make it easy to access the project
# during setup. Once done, you should change this to 'external' or 'internal'.
category: test
@ -37,8 +37,8 @@ environments:
resources:
cpu: 1
memory: 1Gi
- id: prod
projectID: msp-example-prod-TestNewJob
- id: second
projectID: msp-example-second-xxxx
# TODO: We initially provision in 'test' to make it easy to access the project
# during setup. Once done, you should change this to 'external' or 'internal'.
category: test

View File

@ -0,0 +1,41 @@
service:
kind: job
id: msp-example
name: Msp Example
owners:
- core-services
build:
# TODO: Configure the correct image for your job here. If you use a private
# registry like us.gcr.io or Artifact Registry, access will automatically be
# granted for your job to pull the correct image.
image: us.gcr.io/sourcegraph-dev/msp-example
# TODO: Configure where the source code for your job lives here.
source:
repo: github.com/sourcegraph/sourcegraph
dir: cmd/msp-example
environments:
- id: prod
projectID: msp-example-prod-xxxx
# TODO: We initially provision in 'test' to make it easy to access the project
# during setup. Once done, you should change this to 'external' or 'internal'.
category: test
# Specify a strategy for updating the image.
deploy:
type: manual
manual:
tag: insiders
# Specify the schedule at which to run your job.
schedule:
cron: 0 * * * *
deadline: 600 # 10 minutes
# Specify environment configuration your service needs to operate.
env:
SRC_LOG_LEVEL: info
SRC_LOG_FORMAT: json_gcp
# Specify the resources your job gets.
instances:
resources:
cpu: 1
memory: 1Gi

View File

@ -0,0 +1,48 @@
service:
id: msp-example
name: Msp Example
owners:
- core-services
build:
# TODO: Configure the correct image for your service here. If you use a private
# registry like us.gcr.io or Artifact Registry, access will automatically be
# granted for your service to pull the correct image.
image: us.gcr.io/sourcegraph-dev/msp-example
# TODO: Configure where the source code for your service lives here.
source:
repo: github.com/sourcegraph/sourcegraph
dir: cmd/msp-example
environments:
- id: dev
projectID: msp-example-dev-xxxx
# TODO: We initially provision in 'test' to make it easy to access the project
# during setup. Once done, you should change this to 'external' or 'internal'.
category: test
# Specify a deployment strategy for upgrades.
deploy:
type: manual
manual:
tag: insiders
# Specify an externally facing domain.
domain:
type: cloudflare
cloudflare:
subdomain: msp-example
zone: sgdev.org
# Specify environment configuration your service needs to operate.
env:
SRC_LOG_LEVEL: info
SRC_LOG_FORMAT: json_gcp
# Specify how your service should scale.
instances:
resources:
cpu: 1
memory: 1Gi
scaling:
maxCount: 3
minCount: 1
startupProbe:
# Only enable if your service implements MSP /-/healthz conventions.
disabled: true

View File

@ -14,7 +14,7 @@ build:
dir: cmd/msp-example
environments:
- id: dev
projectID: msp-example-dev-TestNewService
projectID: msp-example-dev-xxxx
# TODO: We initially provision in 'test' to make it easy to access the project
# during setup. Once done, you should change this to 'external' or 'internal'.
category: test
@ -44,8 +44,8 @@ environments:
startupProbe:
# Only enable if your service implements MSP /-/healthz conventions.
disabled: true
- id: prod
projectID: msp-example-prod-TestNewService
- id: second
projectID: msp-example-second-xxxx
# TODO: We initially provision in 'test' to make it easy to access the project
# during setup. Once done, you should change this to 'external' or 'internal'.
category: test

View File

@ -0,0 +1,48 @@
service:
id: msp-example
name: Msp Example
owners:
- core-services
build:
# TODO: Configure the correct image for your service here. If you use a private
# registry like us.gcr.io or Artifact Registry, access will automatically be
# granted for your service to pull the correct image.
image: us.gcr.io/sourcegraph-dev/msp-example
# TODO: Configure where the source code for your service lives here.
source:
repo: github.com/sourcegraph/sourcegraph
dir: cmd/msp-example
environments:
- id: dev
projectID: msp-example-dev-xxxx
# TODO: We initially provision in 'test' to make it easy to access the project
# during setup. Once done, you should change this to 'external' or 'internal'.
category: test
# Specify a deployment strategy for upgrades.
deploy:
type: manual
manual:
tag: insiders
# Specify an externally facing domain.
domain:
type: cloudflare
cloudflare:
subdomain: msp-example
zone: sgdev.org
# Specify environment configuration your service needs to operate.
env:
SRC_LOG_LEVEL: info
SRC_LOG_FORMAT: json_gcp
# Specify how your service should scale.
instances:
resources:
cpu: 1
memory: 1Gi
scaling:
maxCount: 3
minCount: 1
startupProbe:
# Only enable if your service implements MSP /-/healthz conventions.
disabled: true

View File

@ -0,0 +1,48 @@
service:
id: msp-example
name: Msp Example
owners:
- core-services
build:
# TODO: Configure the correct image for your service here. If you use a private
# registry like us.gcr.io or Artifact Registry, access will automatically be
# granted for your service to pull the correct image.
image: us.gcr.io/sourcegraph-dev/msp-example
# TODO: Configure where the source code for your service lives here.
source:
repo: github.com/sourcegraph/sourcegraph
dir: cmd/msp-example
environments:
- id: prod
projectID: msp-example-prod-xxxx
# TODO: We initially provision in 'test' to make it easy to access the project
# during setup. Once done, you should change this to 'external' or 'internal'.
category: test
# Specify a deployment strategy for upgrades.
deploy:
type: manual
manual:
tag: insiders
# Specify an externally facing domain.
domain:
type: cloudflare
cloudflare:
subdomain: msp-example
zone: sourcegraph.com
# Specify environment configuration your service needs to operate.
env:
SRC_LOG_LEVEL: info
SRC_LOG_FORMAT: json_gcp
# Specify how your service should scale.
instances:
resources:
cpu: 1
memory: 1Gi
scaling:
maxCount: 3
minCount: 1
startupProbe:
# Only enable if your service implements MSP /-/healthz conventions.
disabled: true

View File

@ -0,0 +1,53 @@
service:
id: msp-example
name: Msp Example
owners:
- core-services
build:
# TODO: Configure the correct image for your service here. If you use a private
# registry like us.gcr.io or Artifact Registry, access will automatically be
# granted for your service to pull the correct image.
image: us.gcr.io/sourcegraph-dev/msp-example
# TODO: Configure where the source code for your service lives here.
source:
repo: github.com/sourcegraph/sourcegraph
dir: cmd/msp-example
environments:
- id: prod
projectID: msp-example-prod-xxxx
# TODO: We initially provision in 'test' to make it easy to access the project
# during setup. Once done, you should change this to 'external' or 'internal'.
category: test
# Specify a deployment strategy for upgrades.
deploy:
type: manual
manual:
tag: insiders
# Specify an externally facing domain.
domain:
type: cloudflare
cloudflare:
subdomain: msp-example
zone: sourcegraph.com
# Specify environment configuration your service needs to operate.
env:
SRC_LOG_LEVEL: info
SRC_LOG_FORMAT: json_gcp
# Specify how your service should scale.
instances:
resources:
cpu: 1
memory: 1Gi
scaling:
maxCount: 3
minCount: 1
startupProbe:
# Only enable if your service implements MSP /-/healthz conventions.
disabled: true
- id: second
projectID: msp-example-second-xxxx
# TODO: We initially provision in 'test' to make it easy to access the project
# during setup. Once done, you should change this to 'external' or 'internal'.
category: test
# TODO: Fill out the rest of your configuration here!
# ...

View File

@ -0,0 +1,48 @@
service:
id: msp-example
name: Msp Example
owners:
- core-services
build:
# TODO: Configure the correct image for your service here. If you use a private
# registry like us.gcr.io or Artifact Registry, access will automatically be
# granted for your service to pull the correct image.
image: us.gcr.io/sourcegraph-dev/msp-example
# TODO: Configure where the source code for your service lives here.
source:
repo: github.com/sourcegraph/sourcegraph
dir: cmd/msp-example
environments:
- id: prod
projectID: msp-example-prod-xxxx
# TODO: We initially provision in 'test' to make it easy to access the project
# during setup. Once done, you should change this to 'external' or 'internal'.
category: test
# Specify a deployment strategy for upgrades.
deploy:
type: manual
manual:
tag: insiders
# Specify an externally facing domain.
domain:
type: cloudflare
cloudflare:
subdomain: msp-example
zone: sourcegraph.com
# Specify environment configuration your service needs to operate.
env:
SRC_LOG_LEVEL: info
SRC_LOG_FORMAT: json_gcp
# Specify how your service should scale.
instances:
resources:
cpu: 1
memory: 1Gi
scaling:
maxCount: 3
minCount: 1
startupProbe:
# Only enable if your service implements MSP /-/healthz conventions.
disabled: true