add resource tests
Some checks failed
Lint / golangci-lint (push) Failing after 10m28s
Declarative Tests / test (push) Failing after 28s

This commit is contained in:
Matthew Rich 2024-09-28 01:04:00 -07:00
parent 10e854583f
commit cc3434c7e3
3 changed files with 117 additions and 48 deletions

View File

@ -178,3 +178,64 @@ func TestCliExportTar(t *testing.T) {
assert.Equal(t, []byte("data"), contents) assert.Equal(t, []byte("data"), contents)
} }
} }
func TestResourcesRead(t *testing.T) {
if _, e := os.Stat("./jx"); errors.Is(e, os.ErrNotExist) {
t.Skip("cli not built")
}
assert.Nil(t, TempDir.CreateFile("testread", "data"))
resources := fmt.Sprintf(`
resources:
- type: file
transition: read
attributes:
path: %s
- type: user
transition: read
attributes:
name: nobody
- type: group
transition: read
attributes:
name: wheel
- type: container
transition: read
attributes:
name: builder
- type: container-network
transition: read
attributes:
name: default
- type: container-image
transition: read
attributes:
name: nginx:latest
- type: http
transition: read
attributes:
endpoint: https://gitea.rosskeen.house
- type: route
transition: read
attributes:
to: 0.0.0.0
gateway: 172.17.0.1
interface: eth0
proto: static
scope: global
rtid: all
routetype: local
metric: 100
`, TempDir.FilePath("testread"))
assert.Nil(t, TempDir.CreateFile("resources.jx.yaml", resources))
yaml, cliErr := exec.Command("./jx", "apply", TempDir.FilePath("resources.jx.yaml")).Output()
if cliErr != nil {
slog.Info("Debug CLI error", "error", cliErr, "stderr", cliErr.(*exec.ExitError).Stderr)
}
assert.Nil(t, cliErr)
assert.NotEqual(t, "", string(yaml))
assert.Greater(t, len(yaml), 0)
}

View File

@ -92,6 +92,7 @@ func init() {
ResourceTypes.Register([]string{"container"}, func(u *url.URL) data.Resource { ResourceTypes.Register([]string{"container"}, func(u *url.URL) data.Resource {
c := NewContainer(nil) c := NewContainer(nil)
c.Name = filepath.Join(u.Hostname(), u.Path) c.Name = filepath.Join(u.Hostname(), u.Path)
c.Common.SetParsedURI(u)
return c return c
}) })
} }
@ -106,7 +107,7 @@ func NewContainer(containerClientApi ContainerClient) *Container {
} }
} }
return &Container{ return &Container{
Common: &Common{ resourceType: ContainerTypeName }, Common: NewCommon(ContainerTypeName, false),
apiClient: apiClient, apiClient: apiClient,
} }
} }
@ -155,24 +156,6 @@ func (c *Container) StateMachine() machine.Stater {
return c.stater return c.stater
} }
/*
func (c *Container) URI() string {
return fmt.Sprintf("container://%s", c.Id)
}
func (c *Container) SetURI(uri string) error {
resourceUri, e := url.Parse(uri)
if e == nil {
if resourceUri.Scheme == c.Type() {
c.Name, e = filepath.Abs(filepath.Join(resourceUri.Hostname(), resourceUri.RequestURI()))
} else {
e = fmt.Errorf("%w: %s is not a %s", ErrInvalidResourceURI, uri, c.Type())
}
}
return e
}
*/
func (c *Container) UseConfig(config data.ConfigurationValueGetter) { func (c *Container) UseConfig(config data.ConfigurationValueGetter) {
c.config = config c.config = config
} }
@ -190,6 +173,16 @@ func (c *Container) Notify(m *machine.EventMessage) {
switch m.On { switch m.On {
case machine.ENTERSTATEEVENT: case machine.ENTERSTATEEVENT:
switch m.Dest { switch m.Dest {
case "start_stat":
if statErr := c.ReadStat(); statErr == nil {
if triggerErr := c.StateMachine().Trigger("exists"); triggerErr == nil {
return
}
} else {
if triggerErr := c.StateMachine().Trigger("notexists"); triggerErr == nil {
return
}
}
case "start_read": case "start_read":
if _,readErr := c.Read(ctx); readErr == nil { if _,readErr := c.Read(ctx); readErr == nil {
if triggerErr := c.stater.Trigger("state_read"); triggerErr == nil { if triggerErr := c.stater.Trigger("state_read"); triggerErr == nil {
@ -237,6 +230,10 @@ func (c *Container) Notify(m *machine.EventMessage) {
} }
} }
func (c *Container) ReadStat() (err error) {
return
}
func (c *Container) Apply() error { func (c *Container) Apply() error {
ctx := context.Background() ctx := context.Background()
switch c.Common.State { switch c.Common.State {
@ -421,9 +418,14 @@ func (c *Container) Delete(ctx context.Context) error {
return err return err
} }
func (c *Container) URI() string {
return fmt.Sprintf("%s://%s", c.Type(), c.Name)
}
func (c *Container) Type() string { return "container" } func (c *Container) Type() string { return "container" }
func (c *Container) ResolveId(ctx context.Context) string { func (c *Container) ResolveId(ctx context.Context) string {
c.Common.SetURI(c.URI())
filterArgs := filters.NewArgs() filterArgs := filters.NewArgs()
filterArgs.Add("name", "/"+c.Name) filterArgs.Add("name", "/"+c.Name)
containers, err := c.apiClient.ContainerList(ctx, container.ListOptions{ containers, err := c.apiClient.ContainerList(ctx, container.ListOptions{

View File

@ -219,6 +219,12 @@ func ProcessMachine(sub machine.Subscriber) machine.Stater {
if e := stater.AddSubscription("update", sub); e != nil { if e := stater.AddSubscription("update", sub); e != nil {
return nil return nil
} }
stater.AddTransition("stat", machine.States("*"), "start_stat")
if e := stater.AddSubscription("stat", sub); e != nil {
return nil
}
stater.AddTransition("updated", machine.States("start_update"), "present") stater.AddTransition("updated", machine.States("start_update"), "present")
stater.AddTransition("delete", machine.States("*"), "start_delete") stater.AddTransition("delete", machine.States("*"), "start_delete")
if e := stater.AddSubscription("delete", sub); e != nil { if e := stater.AddSubscription("delete", sub); e != nil {