Repository navigation
fix(rest): refuse props edits on create doors, honour definitions_only, drop tracker numbers #216
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,177 @@ | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| /* | ||
| Copyright 2026 Cozystack contributors. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| package rest | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "errors" | ||
| "io" | ||
| "net/http" | ||
| "strings" | ||
| "testing" | ||
|
|
||
| apiv1 "github.com/cozystack/blockstor/pkg/api/v1" | ||
| "github.com/cozystack/blockstor/pkg/store" | ||
| ) | ||
|
|
||
| // The props-modify fields edit props a definition already has. On a door | ||
| // that creates the definition there is nothing to edit, and answering | ||
| // success while dropping them claimed an edit that never happened. | ||
| func propsModifyCases() map[string]func(override *map[string]string, del, ns *[]string) { | ||
| return map[string]func(override *map[string]string, del, ns *[]string){ | ||
| "override_props": func(o *map[string]string, _, _ *[]string) { *o = map[string]string{"Aux/k": "v"} }, | ||
| "delete_props": func(_ *map[string]string, d, _ *[]string) { *d = []string{"Aux/k"} }, | ||
| "delete_namespaces": func(_ *map[string]string, _, n *[]string) { *n = []string{"Aux"} }, | ||
| } | ||
| } | ||
|
|
||
| func TestRDCreateRefusesPropsModifyFields(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| for field, set := range propsModifyCases() { | ||
| t.Run(field, func(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| st := store.NewInMemory() | ||
| base, stop := startServerWithStore(t, st) | ||
| defer stop() | ||
|
|
||
| body := apiv1.ResourceDefinitionCreate{ | ||
| ResourceDefinition: apiv1.ResourceDefinition{Name: "rd-modify"}, | ||
| } | ||
| set(&body.OverrideProps, &body.DeleteProps, &body.DeleteNamespace) | ||
|
|
||
| assertRefusedNamingField(t, base+"/v1/resource-definitions", body, field) | ||
|
|
||
| _, err := st.ResourceDefinitions().Get(t.Context(), "rd-modify") | ||
| if !errors.Is(err, store.ErrNotFound) { | ||
| t.Fatalf("a refused create left the definition behind: err=%v", err) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestSpawnRefusesPropsModifyFields(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| for field, set := range propsModifyCases() { | ||
| t.Run(field, func(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| st := store.NewInMemory() | ||
| if err := st.ResourceGroups().Create(t.Context(), &apiv1.ResourceGroup{Name: "rg-modify"}); err != nil { | ||
| t.Fatalf("seed group: %v", err) | ||
| } | ||
|
|
||
| base, stop := startServerWithStore(t, st) | ||
| defer stop() | ||
|
|
||
| body := apiv1.ResourceGroupSpawn{ResourceDefinitionName: "rd-modify", VolumeSizes: []int64{4096}} | ||
| set(&body.OverrideProps, &body.DeleteProps, &body.DeleteNamespace) | ||
|
|
||
| assertRefusedNamingField(t, base+"/v1/resource-groups/rg-modify/spawn", body, field) | ||
|
|
||
| _, err := st.ResourceDefinitions().Get(t.Context(), "rd-modify") | ||
| if !errors.Is(err, store.ErrNotFound) { | ||
| t.Fatalf("a refused spawn left the definition behind: err=%v", err) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| // `linstor rg spawn --definition-only` asks for the definition and its | ||
| // volumes and no placement. The handler placed replicas anyway. | ||
| func TestSpawnDefinitionsOnlyPlacesNothing(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| st := store.NewInMemory() | ||
| ctx := t.Context() | ||
|
|
||
| if err := st.ResourceGroups().Create(ctx, &apiv1.ResourceGroup{ | ||
| Name: "rg-defs", | ||
| SelectFilter: apiv1.AutoSelectFilter{PlaceCount: 2, StoragePool: "pool"}, | ||
| }); err != nil { | ||
| t.Fatalf("seed group: %v", err) | ||
| } | ||
|
|
||
| for _, node := range []string{"n1", "n2"} { | ||
| if err := st.StoragePools().Create(ctx, &apiv1.StoragePool{ | ||
| StoragePoolName: "pool", NodeName: node, ProviderKind: apiv1.StoragePoolKindLVMThin, | ||
| FreeCapacity: 1_000_000, TotalCapacity: 10_000_000, | ||
| }); err != nil { | ||
| t.Fatalf("seed pool: %v", err) | ||
| } | ||
| } | ||
|
|
||
| base, stop := startServerWithStore(t, st) | ||
| defer stop() | ||
|
|
||
| raw, err := json.Marshal(apiv1.ResourceGroupSpawn{ | ||
| ResourceDefinitionName: "rd-defs", | ||
| VolumeSizes: []int64{4096}, | ||
| DefinitionsOnly: true, | ||
| }) | ||
| if err != nil { | ||
| t.Fatalf("marshal: %v", err) | ||
| } | ||
|
|
||
| resp := httpPost(t, base+"/v1/resource-groups/rg-defs/spawn", raw) | ||
| defer func() { _ = resp.Body.Close() }() | ||
|
|
||
| if resp.StatusCode != http.StatusCreated { | ||
| b, _ := io.ReadAll(resp.Body) | ||
| t.Fatalf("status %d, want 201: %s", resp.StatusCode, b) | ||
| } | ||
|
|
||
| vds, err := st.VolumeDefinitions().List(ctx, "rd-defs") | ||
| if err != nil || len(vds) != 1 { | ||
| t.Fatalf("volume definitions: %+v, err=%v; want one", vds, err) | ||
| } | ||
|
|
||
| replicas, err := st.Resources().ListByDefinition(ctx, "rd-defs") | ||
| if err != nil { | ||
| t.Fatalf("list replicas: %v", err) | ||
| } | ||
|
|
||
| if len(replicas) != 0 { | ||
| t.Fatalf("definitions_only spawn placed %d replicas, want none", len(replicas)) | ||
| } | ||
| } | ||
|
|
||
| func assertRefusedNamingField(t *testing.T, url string, body any, field string) { | ||
| t.Helper() | ||
|
|
||
| raw, err := json.Marshal(body) | ||
| if err != nil { | ||
| t.Fatalf("marshal: %v", err) | ||
| } | ||
|
|
||
| resp := httpPost(t, url, raw) | ||
| defer func() { _ = resp.Body.Close() }() | ||
|
|
||
| got, _ := io.ReadAll(resp.Body) | ||
|
|
||
| if resp.StatusCode != http.StatusBadRequest { | ||
| t.Fatalf("status %d, want 400: %s", resp.StatusCode, got) | ||
| } | ||
|
|
||
| if !strings.Contains(string(got), field) { | ||
| t.Fatalf("the refusal does not name %s: %s", field, got) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| /* | ||
| Copyright 2026 Cozystack contributors. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| package rest | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing |
||
|
|
||
| import ( | ||
| "fmt" | ||
| "go/ast" | ||
| "go/parser" | ||
| "go/token" | ||
| "io/fs" | ||
| "path/filepath" | ||
| "regexp" | ||
| "strconv" | ||
| "strings" | ||
| "testing" | ||
| ) | ||
|
|
||
| // internalTrackerRef matches the project's internal tracker spellings in | ||
| // any case, "Bug 174", "Bug-020", "BUG-048" and "Phase 12". They mean nothing to an operator | ||
| // reading an error, an event or a log line, so no string literal in the | ||
| // shipped code carries one; comments may. | ||
| var internalTrackerRef = regexp.MustCompile(`(?i)\bBug[- ][0-9]|\bPhase [0-9]`) | ||
|
|
||
| func TestShippedStringsCarryNoInternalTrackerNumbers(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| var hits []string | ||
|
|
||
| for _, root := range []string{"../../pkg", "../../internal", "../../cmd"} { | ||
| err := filepath.WalkDir(root, func(path string, d fs.DirEntry, err error) error { | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| if d.IsDir() || !strings.HasSuffix(path, ".go") || strings.HasSuffix(path, "_test.go") { | ||
| return nil | ||
| } | ||
|
|
||
| fset := token.NewFileSet() | ||
|
|
||
| file, parseErr := parser.ParseFile(fset, path, nil, parser.SkipObjectResolution) | ||
| if parseErr != nil { | ||
| return fmt.Errorf("parse %s: %w", path, parseErr) | ||
| } | ||
|
|
||
| ast.Inspect(file, func(n ast.Node) bool { | ||
| lit, ok := n.(*ast.BasicLit) | ||
| if !ok || lit.Kind != token.STRING { | ||
| return true | ||
| } | ||
|
|
||
| value, unquoteErr := strconv.Unquote(lit.Value) | ||
| if unquoteErr != nil { | ||
| value = lit.Value | ||
| } | ||
|
|
||
| if internalTrackerRef.MatchString(value) { | ||
| hits = append(hits, fset.Position(lit.Pos()).String()+": "+value) | ||
| } | ||
|
|
||
| return true | ||
| }) | ||
|
|
||
| return nil | ||
| }) | ||
| if err != nil { | ||
| t.Fatalf("walk %s: %v", root, err) | ||
| } | ||
| } | ||
|
|
||
| for _, hit := range hits { | ||
| t.Errorf("string literal carries an internal tracker number: %s", hit) | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing
// SPDX-License-Identifier: Apache-2.0header.