-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added a new handler and respective tests to get affiliate data in a cluster. Signed-off-by: Rohit Dandamudi <rohit.dandamudi@siderolabs.com>
- Loading branch information
Rohit Dandamudi
authored and
Rohit Dandamudi
committed
Nov 23, 2021
1 parent
4906c98
commit 52b44de
Showing
5 changed files
with
163 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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 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 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 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,41 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<title> Inspect Cluster </title> | ||
</head> | ||
<body> | ||
<form class="form-group" action="/inspect" method="GET"> | ||
<div class="form-group"> | ||
<label for="clusterID">Enter Cluster ID :</label> | ||
<input type="text" id="clusterID" name="clusterID" class = "form-control" placeholder="sjdhfUG@%HKA^Srasfjskfj9987sfkj"><br> | ||
</div> | ||
<button type="submit" class="btn btn-default">Submit</button> | ||
</form> | ||
<br><hr> | ||
{{ if .ClusterID }} | ||
<h4>The affiliates associated with given cluster ID {{.ClusterID}}: </h4> | ||
{{if not .Affiliates}} | ||
<p>No Affiliate data found</p> | ||
{{end}} | ||
<ol> | ||
{{range .Affiliates}} | ||
<li class="affiliateID">Affliate ID: {{.ID}} </li> | ||
<ul> | ||
<li class="affiliateData"> | ||
<span style="width:90%; word-wrap:break-word; display:inline-block;"> | ||
Affliate Data: {{.Data | printf "%x"}} | ||
</span> | ||
</li> | ||
<li class="affiliateEndpoints">Affiliate Endpoints: | ||
<ul> | ||
{{range .Endpoints}} | ||
<li> {{. | printf "%x"}}</li> | ||
{{end}} | ||
</ul> | ||
</li> | ||
</ul> | ||
{{end}} | ||
</ol> | ||
{{end}} | ||
</body> | ||
</html> |
This file contains 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,68 @@ | ||
// Copyright (c) 2021 Sidero Labs, Inc. | ||
// | ||
// Use of this software is governed by the Business Source License | ||
// included in the LICENSE file. | ||
|
||
package server_test | ||
|
||
import ( | ||
"context" | ||
"io" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
"go.uber.org/zap/zaptest" | ||
|
||
"github.com/talos-systems/discovery-service/internal/landing" | ||
"github.com/talos-systems/discovery-service/internal/state" | ||
) | ||
|
||
// TestInspectHandler tests the /inspect endpoint. | ||
// Tests at 3001 port filled with dummy cluster/affiliate data. | ||
func TestInspectHanlder(t *testing.T) { | ||
logger := zaptest.NewLogger(t) | ||
now := time.Now() | ||
stateInstance := state.NewState(logger) | ||
testCluster := stateInstance.GetCluster("fake1") | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
|
||
t.Cleanup(cancel) | ||
|
||
// add affiliates to the cluster "fake1" | ||
err := testCluster.WithAffiliate("af1", func(affiliate *state.Affiliate) error { | ||
affiliate.Update([]byte("data1"), now.Add(time.Minute)) | ||
|
||
return nil | ||
}) | ||
require.NoError(t, err) | ||
|
||
err = testCluster.WithAffiliate("af2", func(affiliate *state.Affiliate) error { | ||
affiliate.Update([]byte("data2"), now.Add(time.Minute)) | ||
|
||
return nil | ||
}) | ||
require.NoError(t, err) | ||
|
||
ts := httptest.NewServer(landing.Handler(stateInstance, logger)) | ||
defer ts.Close() | ||
|
||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, ts.URL+"/inspect?clusterID=fake1", nil) | ||
require.NoError(t, err) | ||
|
||
res, err := http.DefaultClient.Do(req) | ||
require.NoError(t, err) | ||
|
||
inspectPage, err := io.ReadAll(res.Body) | ||
require.NoError(t, err) | ||
|
||
err = res.Body.Close() | ||
require.NoError(t, err) | ||
|
||
assert.Equal(t, 200, res.StatusCode) | ||
|
||
t.Log(string(inspectPage)) | ||
} |