Skip to content

Commit

Permalink
test: adds integration test for policy command
Browse files Browse the repository at this point in the history
  • Loading branch information
pallabpain committed Jul 12, 2024
1 parent c2c08d9 commit 5a0be76
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 1 deletion.
1 change: 1 addition & 0 deletions .github/workflows/test-integration.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ jobs:
- TestNodeExpireCommand
- TestNodeRenameCommand
- TestNodeMoveCommand
- TestPolicyCommand
- TestDERPServerScenario
- TestPingAllByIP
- TestPingAllByIPPublicDERP
Expand Down
80 changes: 80 additions & 0 deletions integration/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1597,3 +1597,83 @@ func TestNodeMoveCommand(t *testing.T) {

assert.Equal(t, node.GetUser().GetName(), "old-user")
}

func TestPolicyCommand(t *testing.T) {
IntegrationSkip(t)
t.Parallel()

scenario, err := NewScenario(dockertestMaxWait())
assertNoErr(t, err)
defer scenario.Shutdown()

spec := map[string]int{
"policy-user": 0,
}

err = scenario.CreateHeadscaleEnv(
spec,
[]tsic.Option{},
hsic.WithTestName("clins"),
hsic.WithConfigEnv(map[string]string{
"HEADSCALE_POLICY_MODE": "database",
}),
)
assertNoErr(t, err)

headscale, err := scenario.Headscale()
assertNoErr(t, err)

p := policy.ACLPolicy{
ACLs: []policy.ACL{
{
Action: "accept",
Sources: []string{"*"},
Destinations: []string{"*:*"},
},
},
TagOwners: map[string][]string{
"tag:exists": {"policy-user"},
},
}

pBytes, _ := json.Marshal(p)

policyFilePath := "/etc/headscale/policy.json"

err = headscale.WriteFile(policyFilePath, pBytes)
assertNoErr(t, err)

// No policy is present at this time.
// Add a new policy from a file.
_, err = headscale.Execute(
[]string{
"headscale",
"policy",
"set",
"-f",
policyFilePath,
},
)

assertNoErr(t, err)

// Get the current policy and check
// if it is the same as the one we set.
var output *policy.ACLPolicy
err = executeAndUnmarshal(
headscale,
[]string{
"headscale",
"policy",
"get",
"--output",
"json",
},
&output,
)
assertNoErr(t, err)

assert.Len(t, output.TagOwners, 1)
assert.Len(t, output.ACLs, 1)
assert.Equal(t, output.TagOwners["tag:exists"], []string{"policy-user"})
}
4 changes: 3 additions & 1 deletion integration/control.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
package integration

import (
v1 "github.com/juanfont/headscale/gen/go/headscale/v1"
"github.com/ory/dockertest/v3"

v1 "github.com/juanfont/headscale/gen/go/headscale/v1"
)

type ControlServer interface {
Shutdown() error
SaveLog(string) error
SaveProfile(string) error
Execute(command []string) (string, error)
WriteFile(path string, content []byte) error
ConnectToNetwork(network *dockertest.Network) error
GetHealthEndpoint() string
GetEndpoint() string
Expand Down

0 comments on commit 5a0be76

Please sign in to comment.