Skip to content

Commit e0ab538

Browse files
committed
Switches to Imposter for mock config generation.
Uses path hints to guess availability path.
1 parent a91fe5e commit e0ab538

8 files changed

Lines changed: 91 additions & 83 deletions

File tree

cmd/mock.go

Lines changed: 38 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,17 @@ limitations under the License.
1717
package cmd
1818

1919
import (
20-
"fmt"
2120
"gatehill.io/imposter/engine"
2221
"gatehill.io/imposter/engine/docker"
22+
imposterfileutil "gatehill.io/imposter/fileutil"
23+
"gatehill.io/imposter/impostermodel"
2324
"github.com/sirupsen/logrus"
2425
"github.com/spf13/cobra"
2526
"opendeps.org/opendeps/fileutil"
2627
"opendeps.org/opendeps/model"
2728
"os"
2829
"os/signal"
2930
"path/filepath"
30-
"strings"
3131
"syscall"
3232
)
3333

@@ -53,8 +53,8 @@ by this tool.`,
5353
logrus.Debugf("reading opendeps manifest: %v\n", manifestPath)
5454
manifest := model.Parse(manifestPath)
5555

56-
bundleManifest(stagingDir, manifestPath)
57-
generateMockConfig(stagingDir, manifestPath, manifest)
56+
bundleManifest(stagingDir, manifestPath, flagForceOverwrite)
57+
bundleSpecs(stagingDir, manifestPath, manifest, flagForceOverwrite)
5858

5959
mockEngine := docker.BuildEngine(stagingDir, engine.StartOptions{
6060
Port: 8080,
@@ -68,50 +68,51 @@ by this tool.`,
6868
},
6969
}
7070

71-
func bundleManifest(stagingDir string, manifestPath string) {
71+
func init() {
72+
rootCmd.AddCommand(mockCmd)
73+
}
74+
75+
func bundleManifest(stagingDir string, manifestPath string, forceOverwrite bool) {
7276
logrus.Debugf("bundling manifest: %v", manifestPath)
7377
err := fileutil.CopyContent(manifestPath, filepath.Join(stagingDir, "opendeps.yaml"))
7478
if err != nil {
7579
logrus.Fatal(err)
7680
}
77-
writeManifestOpenApiSpec(stagingDir)
78-
}
7981

80-
func init() {
81-
rootCmd.AddCommand(mockCmd)
82+
specFileName := "opendeps-openapi-gen.yaml"
83+
writeManifestSpec(stagingDir, specFileName)
84+
85+
resources := []impostermodel.Resource{
86+
{
87+
Path: "/.well-known/opendeps/manifest.yaml",
88+
Method: "GET",
89+
Response: &impostermodel.ResponseConfig{
90+
StaticFile: "opendeps.yaml",
91+
},
92+
},
93+
}
94+
writeMockConfig(filepath.Join(stagingDir, specFileName), resources, forceOverwrite)
8295
}
8396

84-
func generateMockConfig(stagingDir string, specFile string, manifest *model.OpenDeps) string {
97+
func bundleSpecs(stagingDir string, manifestPath string, manifest *model.OpenDeps, forceOverwrite bool) string {
8598
for _, dependency := range manifest.Dependencies {
86-
openapiNormalisedPath := makeAbsoluteRelativeToFile(dependency.Spec, specFile)
87-
logrus.Debugf("bundling openapi spec: %v\n", openapiNormalisedPath)
99+
specNormalisedPath := fileutil.MakeAbsoluteRelativeToFile(dependency.Spec, manifestPath)
100+
logrus.Debugf("bundling openapi spec: %v\n", specNormalisedPath)
88101

89-
openapiFilename := filepath.Base(openapiNormalisedPath)
90-
openapiDestFile := filepath.Join(stagingDir, openapiFilename)
91-
err := fileutil.CopyContent(openapiNormalisedPath, openapiDestFile)
102+
specDestPath := filepath.Join(stagingDir, filepath.Base(specNormalisedPath))
103+
err := fileutil.CopyContent(specNormalisedPath, specDestPath)
92104
if err != nil {
93105
panic(err)
94106
}
95107

96-
writeMockConfig(stagingDir, openapiFilename)
108+
writeMockConfig(specDestPath, nil, forceOverwrite)
97109
}
98110
return stagingDir
99111
}
100112

101-
func makeAbsoluteRelativeToFile(filePath string, specFile string) string {
102-
specDir := filepath.Dir(specFile)
103-
104-
var openapiNormalisedPath string
105-
if strings.HasPrefix(filePath, "./") {
106-
openapiNormalisedPath = filepath.Join(specDir, strings.TrimPrefix(filePath, "."))
107-
} else {
108-
openapiNormalisedPath = filePath
109-
}
110-
return openapiNormalisedPath
111-
}
112-
113-
func writeManifestOpenApiSpec(configDir string) {
114-
specFileName := "opendeps-openapi-gen.yaml"
113+
// writeManifestSpec creates an OpenAPI spec describing the well known endpoint
114+
// that serves the manifest.
115+
func writeManifestSpec(configDir string, specFileName string) {
115116
specFile, err := os.Create(filepath.Join(configDir, specFileName))
116117
if err != nil {
117118
panic(err)
@@ -141,40 +142,21 @@ paths:
141142
if err != nil {
142143
panic(err)
143144
}
144-
145-
err = specFile.Sync()
146-
if err != nil {
147-
panic(err)
148-
}
149-
150-
writeMockConfig(configDir, specFileName)
151145
}
152146

153-
func writeMockConfig(configDir string, openapiFilename string) {
154-
configFile, err := os.Create(filepath.Join(configDir, fmt.Sprintf("%v-config.yaml", openapiFilename)))
147+
func writeMockConfig(specFilePath string, resources []impostermodel.Resource, forceOverwrite bool) {
148+
configFilePath := imposterfileutil.GenerateFilePathAdjacentToFile(specFilePath, "-config.yaml", forceOverwrite)
149+
configFile, err := os.Create(configFilePath)
155150
if err != nil {
156151
panic(err)
157152
}
158153
defer configFile.Close()
159154

160-
config := fmt.Sprintf(`---
161-
plugin: openapi
162-
specFile: "%v"
163-
164-
resources:
165-
- path: /.well-known/opendeps/manifest.yaml
166-
method: GET
167-
response:
168-
staticFile: opendeps.yaml
169-
template: true
170-
`, openapiFilename)
171-
172-
_, err = configFile.WriteString(config)
173-
if err != nil {
174-
panic(err)
175-
}
155+
config := impostermodel.GenerateConfig(specFilePath, resources, impostermodel.ConfigGenerationOptions{
156+
ScriptEngine: impostermodel.ScriptEngineNone,
157+
})
176158

177-
err = configFile.Sync()
159+
_, err = configFile.Write(config)
178160
if err != nil {
179161
panic(err)
180162
}

cmd/scaffold.go

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,7 @@ func scaffoldManifest(specDir string, forceOverwrite bool) {
9191
}
9292
}
9393

94-
manifestFileName := fileutil.GenerateFilenameAdjacentToFile(specDir, filepath.Join(specDir, "opendeps"), ".yaml", forceOverwrite)
95-
manifestPath := filepath.Join(specDir, manifestFileName)
96-
94+
manifestPath := fileutil.GenerateFilePathAdjacentToFile(filepath.Join(specDir, "opendeps"), ".yaml", forceOverwrite)
9795
file, err := os.Create(manifestPath)
9896
if err != nil {
9997
logrus.Fatalf("error creating opendeps manifest file: %v: %v", manifestPath, err)
@@ -110,10 +108,29 @@ func scaffoldManifest(specDir string, forceOverwrite bool) {
110108
}
111109

112110
func determineAvailabilityPath(spec *openapi.PartialModel) string {
111+
var getPaths []string
113112
for path, operations := range spec.Paths {
114113
if _, found := operations["get"]; found {
115-
return path
114+
getPaths = append(getPaths, path)
115+
}
116+
}
117+
if len(getPaths) == 1 {
118+
return getPaths[0]
119+
} else {
120+
for _, path := range getPaths {
121+
for _, pathHint := range getAvailabilityPathHints() {
122+
if path == pathHint {
123+
return path
124+
}
125+
}
116126
}
117127
}
118128
return "/"
119129
}
130+
131+
func getAvailabilityPathHints() []string {
132+
return []string{
133+
"/healthz",
134+
"/system/status",
135+
}
136+
}

cmd/test.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ func testDependencies(manifestPath string) (successful int, tested int) {
9292
return available, len(manifest.Dependencies)
9393
}
9494

95-
func testDependency(specFile string, depName string, dep model.Dependency) error {
95+
func testDependency(manifestPath string, depName string, dep model.Dependency) error {
9696
if "" != dep.Availability.Security {
9797
logrus.Warnf("security configuration for availability endpoints is not supported\n")
9898
}
@@ -104,7 +104,7 @@ func testDependency(specFile string, depName string, dep model.Dependency) error
104104

105105
} else if "" != dep.Availability.Path {
106106
// relative - use openapi spec servers as base path
107-
basePath, err := determineBasePath(specFile, depName, dep)
107+
basePath, err := determineBasePath(manifestPath, depName, dep)
108108
if err != nil {
109109
return err
110110
}
@@ -128,24 +128,24 @@ func testDependency(specFile string, depName string, dep model.Dependency) error
128128
return nil
129129
}
130130

131-
func determineBasePath(specFile string, depName string, dependency model.Dependency) (string, error) {
131+
func determineBasePath(manifestPath string, depName string, dependency model.Dependency) (string, error) {
132132
if serverUrl, found := flagServers[depName]; found {
133133
logrus.Debugf("determined server [%v] from overrides", serverUrl)
134134
return serverUrl, nil
135135

136136
} else {
137-
openapiNormalisedPath := makeAbsoluteRelativeToFile(dependency.Spec, specFile)
138-
openapiSpec, err := openapi.Parse(openapiNormalisedPath)
137+
specNormalisedPath := fileutil.MakeAbsoluteRelativeToFile(dependency.Spec, manifestPath)
138+
openapiSpec, err := openapi.Parse(specNormalisedPath)
139139
if err != nil {
140-
return "", fmt.Errorf("failed to parse spec [%v]: %v\n", openapiNormalisedPath, err)
140+
return "", fmt.Errorf("failed to parse spec [%v]: %v\n", specNormalisedPath, err)
141141
}
142142
if len(openapiSpec.Servers) == 0 {
143-
return "", fmt.Errorf("no servers found in spec [%v]\n", openapiNormalisedPath)
143+
return "", fmt.Errorf("no servers found in spec [%v]\n", specNormalisedPath)
144144
} else if len(openapiSpec.Servers) > 1 {
145-
logrus.Warnf("more than 1 server found in spec [%v] - using first\n", openapiNormalisedPath)
145+
logrus.Warnf("more than 1 server found in spec [%v] - using first\n", specNormalisedPath)
146146
}
147147
serverUrl := openapiSpec.Servers[0].Url
148-
logrus.Debugf("determined server [%v] from openapi spec [%v]", serverUrl, openapiNormalisedPath)
148+
logrus.Debugf("determined server [%v] from openapi spec [%v]", serverUrl, specNormalisedPath)
149149
return serverUrl, nil
150150
}
151151
}

fileutil/fileutil.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"log"
2525
"net/http"
2626
"os"
27+
"path/filepath"
2728
"strings"
2829
)
2930

@@ -97,10 +98,6 @@ func writeToFile(source io.Reader, dest string) error {
9798
return fmt.Errorf("error : %s", err.Error())
9899
}
99100

100-
err = destFile.Sync()
101-
if err != nil {
102-
return fmt.Errorf("error : %s", err.Error())
103-
}
104101
return nil
105102
}
106103

@@ -115,3 +112,15 @@ func fetchFile(source string) (io.ReadCloser, error) {
115112
}
116113
return os.Open(normalisedPath)
117114
}
115+
116+
func MakeAbsoluteRelativeToFile(inputPath string, relativeToPath string) string {
117+
specDir := filepath.Dir(relativeToPath)
118+
119+
var normalisedPath string
120+
if strings.HasPrefix(inputPath, "./") {
121+
normalisedPath = filepath.Join(specDir, strings.TrimPrefix(inputPath, "."))
122+
} else {
123+
normalisedPath = inputPath
124+
}
125+
return normalisedPath
126+
}
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@ func getDefaultSearchFilenames() []string {
3535
// If args is not empty, the path is made absolute, followed by
3636
// a search for well-known filenames, or a fully qualified
3737
// file path if specified.
38-
func FindManifestFile(args []string) (specFile string, err error) {
38+
func FindManifestFile(args []string) (manifestPath string, err error) {
3939
if len(args) == 0 {
4040
wd, _ := os.Getwd()
41-
return findSpecInDir(wd)
41+
return findManifestInDir(wd)
4242
} else {
4343
absPath, _ := filepath.Abs(args[0])
4444
fileInfo, err := os.Stat(absPath)
@@ -50,13 +50,13 @@ func FindManifestFile(args []string) (specFile string, err error) {
5050
}
5151
}
5252
if fileInfo.IsDir() {
53-
return findSpecInDir(absPath)
53+
return findManifestInDir(absPath)
5454
}
5555
return absPath, nil
5656
}
5757
}
5858

59-
func findSpecInDir(dir string) (specFile string, err error) {
59+
func findManifestInDir(dir string) (manifestPath string, err error) {
6060
for _, defaultSearchFilename := range getDefaultSearchFilenames() {
6161
searchFilePath := filepath.Join(dir, defaultSearchFilename)
6262
if _, err := os.Stat(searchFilePath); err != nil {

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ module opendeps.org/opendeps
22

33
go 1.17
44

5-
replace gatehill.io/imposter => github.com/gatehill/imposter-cli v0.4.2
5+
replace gatehill.io/imposter => github.com/gatehill/imposter-cli v0.4.5
66

77
require (
8-
gatehill.io/imposter v0.4.2
8+
gatehill.io/imposter v0.4.5
99
github.com/fsnotify/fsnotify v1.5.1 // indirect
1010
github.com/hashicorp/hcl v1.0.0 // indirect
1111
github.com/inconshreveable/mousetrap v1.0.0 // indirect

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -279,8 +279,8 @@ github.com/fsnotify/fsnotify v1.5.1 h1:mZcQUHVQUQWoPXXtuf9yuEXKudkV2sx1E06UadKWp
279279
github.com/fsnotify/fsnotify v1.5.1/go.mod h1:T3375wBYaZdLLcVNkcVbzGHY7f1l/uK5T5Ai1i3InKU=
280280
github.com/fullsailor/pkcs7 v0.0.0-20190404230743-d7302db945fa/go.mod h1:KnogPXtdwXqoenmZCw6S+25EAm2MkxbG0deNDu4cbSA=
281281
github.com/garyburd/redigo v0.0.0-20150301180006-535138d7bcd7/go.mod h1:NR3MbYisc3/PwhQ00EMzDiPmrwpPxAn5GI05/YaO1SY=
282-
github.com/gatehill/imposter-cli v0.4.2 h1:yCyVRA+qJZ/tdD0VmOKle2TW2UzMDt8G6oC8m3yIrh0=
283-
github.com/gatehill/imposter-cli v0.4.2/go.mod h1:1cvFX7e8yvBhldJKnIMFV276eLsbtO6LmrHGx4bN+IQ=
282+
github.com/gatehill/imposter-cli v0.4.5 h1:F7WifoC/oZql390jtJS5KM1H1atSBePvet0ht3PCZWs=
283+
github.com/gatehill/imposter-cli v0.4.5/go.mod h1:1cvFX7e8yvBhldJKnIMFV276eLsbtO6LmrHGx4bN+IQ=
284284
github.com/ghodss/yaml v0.0.0-20150909031657-73d445a93680/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
285285
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
286286
github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU=

model/parser.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ import (
2222
"io/ioutil"
2323
)
2424

25-
func Parse(specFile string) *OpenDeps {
26-
raw, err := ioutil.ReadFile(specFile)
25+
func Parse(manifestPath string) *OpenDeps {
26+
raw, err := ioutil.ReadFile(manifestPath)
2727
if err != nil {
2828
logrus.Fatalln(err)
2929
}

0 commit comments

Comments
 (0)