Skip to content

Commit 361ae3b

Browse files
authored
chore: add fmt makefile target and go tools (#356)
Go formatting based on Otel collector's approach: https://github.com/open-telemetry/opentelemetry-collector The PR has been committed as: - Add fmt go tools and refactor the corresponding make targets - Add CI check - Push formatted files
1 parent e718373 commit 361ae3b

26 files changed

+134
-93
lines changed

.github/workflows/build-and-test.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,10 @@ jobs:
6565
with:
6666
path: /home/runner/.cache/go-build
6767
key: go-build-unittest-${{ runner.os }}-${{ hashFiles('**/go.mod', '**/go.sum') }}
68+
- name: Go fmt
69+
run: |
70+
make fmt
71+
git diff --exit-code || (echo 'Code base is not Go formatted, please run "make fmt" and commit the changes in this PR.' && exit 1)
6872
- name: Build and Test
6973
run: make
7074

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,6 @@
1818
coverage.out
1919
coverage.txt
2020
coverage.html
21+
22+
# Project tools
23+
.tools/

client/client.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99

1010
// OpAMPClient is an interface representing the client side of the OpAMP protocol.
1111
type OpAMPClient interface {
12-
1312
// Start the client and begin attempts to connect to the Server. Once connection
1413
// is established the client will attempt to maintain it by reconnecting if
1514
// the connection is lost. All failed connection attempts will be reported via

client/clientimpl_test.go

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,6 @@ func createRemoteConfig() *protobufs.AgentRemoteConfig {
468468

469469
func TestFirstStatusReport(t *testing.T) {
470470
testClients(t, func(t *testing.T, client OpAMPClient) {
471-
472471
remoteConfig := createRemoteConfig()
473472

474473
// Start a Server.
@@ -629,13 +628,11 @@ func TestSetEffectiveConfig(t *testing.T) {
629628
// Shutdown the client.
630629
err := client.Stop(context.Background())
631630
assert.NoError(t, err)
632-
633631
})
634632
}
635633

636634
func TestSetAgentDescription(t *testing.T) {
637635
testClients(t, func(t *testing.T, client OpAMPClient) {
638-
639636
// Start a Server.
640637
srv := internal.StartMockServer(t)
641638
var rcvAgentDescr atomic.Value
@@ -927,7 +924,6 @@ func TestClientRequestConnectionSettings(t *testing.T) {
927924

928925
func TestReportAgentDescription(t *testing.T) {
929926
testClients(t, func(t *testing.T, client OpAMPClient) {
930-
931927
// Start a Server.
932928
srv := internal.StartMockServer(t)
933929
srv.EnableExpectMode()
@@ -990,7 +986,6 @@ func TestReportAgentDescription(t *testing.T) {
990986

991987
func TestReportAgentHealth(t *testing.T) {
992988
testClients(t, func(t *testing.T, client OpAMPClient) {
993-
994989
// Start a Server.
995990
srv := internal.StartMockServer(t)
996991
srv.EnableExpectMode()
@@ -1063,7 +1058,6 @@ func TestReportAgentHealth(t *testing.T) {
10631058

10641059
func TestReportEffectiveConfig(t *testing.T) {
10651060
testClients(t, func(t *testing.T, client OpAMPClient) {
1066-
10671061
// Start a Server.
10681062
srv := internal.StartMockServer(t)
10691063
srv.EnableExpectMode()
@@ -1131,7 +1125,6 @@ func TestReportEffectiveConfig(t *testing.T) {
11311125

11321126
func verifyRemoteConfigUpdate(t *testing.T, successCase bool, expectStatus *protobufs.RemoteConfigStatus) {
11331127
testClients(t, func(t *testing.T, client OpAMPClient) {
1134-
11351128
// Start a Server.
11361129
srv := internal.StartMockServer(t)
11371130
srv.EnableExpectMode()
@@ -1235,7 +1228,6 @@ func verifyRemoteConfigUpdate(t *testing.T, successCase bool, expectStatus *prot
12351228
}
12361229

12371230
func TestRemoteConfigUpdate(t *testing.T) {
1238-
12391231
tests := []struct {
12401232
name string
12411233
success bool
@@ -1280,7 +1272,8 @@ const packageUpdateErrorMsg = "cannot update packages"
12801272

12811273
func assertPackageStatus(t *testing.T,
12821274
testCase packageTestCase,
1283-
msg *protobufs.AgentToServer) (*protobufs.ServerToAgent, bool) {
1275+
msg *protobufs.AgentToServer,
1276+
) (*protobufs.ServerToAgent, bool) {
12841277
expectedStatusReceived := false
12851278

12861279
status := msg.PackageStatuses
@@ -1327,7 +1320,6 @@ func assertPackageStatus(t *testing.T,
13271320

13281321
func verifyUpdatePackages(t *testing.T, testCase packageTestCase) {
13291322
testClients(t, func(t *testing.T, client OpAMPClient) {
1330-
13311323
// Start a Server.
13321324
srv := internal.StartMockServer(t)
13331325
srv.EnableExpectMode()
@@ -1383,7 +1375,8 @@ func verifyUpdatePackages(t *testing.T, testCase packageTestCase) {
13831375
// ---> Server
13841376
// Wait for the expected package statuses to be received.
13851377
srv.EventuallyExpect("full PackageStatuses", func(msg *protobufs.AgentToServer) (*protobufs.ServerToAgent,
1386-
bool) {
1378+
bool,
1379+
) {
13871380
return assertPackageStatus(t, testCase, msg)
13881381
})
13891382

@@ -1512,7 +1505,6 @@ func createPackageTestCase(name string, downloadSrv *httptest.Server) packageTes
15121505
}
15131506

15141507
func TestUpdatePackages(t *testing.T) {
1515-
15161508
downloadSrv := createDownloadSrv(t)
15171509
defer downloadSrv.Close()
15181510

@@ -1656,14 +1648,12 @@ func TestMissingPackagesStateProvider(t *testing.T) {
16561648
}
16571649

16581650
func TestOfferUpdatedVersion(t *testing.T) {
1659-
16601651
downloadSrv := createDownloadSrv(t)
16611652
defer downloadSrv.Close()
16621653

16631654
testCase := createPackageTestCase("offer new version", downloadSrv)
16641655

16651656
testClients(t, func(t *testing.T, client OpAMPClient) {
1666-
16671657
localPackageState := internal.NewInMemPackagesStore()
16681658
srv := internal.StartMockServer(t)
16691659
srv.EnableExpectMode()
@@ -1706,7 +1696,8 @@ func TestOfferUpdatedVersion(t *testing.T) {
17061696
// ---> Server
17071697
// Wait for the expected package statuses to be received.
17081698
srv.EventuallyExpect("full PackageStatuses", func(msg *protobufs.AgentToServer) (*protobufs.ServerToAgent,
1709-
bool) {
1699+
bool,
1700+
) {
17101701
return assertPackageStatus(t, testCase, msg)
17111702
})
17121703

@@ -1733,7 +1724,8 @@ func TestOfferUpdatedVersion(t *testing.T) {
17331724
// ---> Server
17341725
// Wait for the expected package statuses to be received.
17351726
srv.EventuallyExpect("full PackageStatuses updated version", func(msg *protobufs.AgentToServer) (*protobufs.ServerToAgent,
1736-
bool) {
1727+
bool,
1728+
) {
17371729
return assertPackageStatus(t, testCase, msg)
17381730
})
17391731

@@ -1748,7 +1740,6 @@ func TestOfferUpdatedVersion(t *testing.T) {
17481740

17491741
func TestReportCustomCapabilities(t *testing.T) {
17501742
testClients(t, func(t *testing.T, client OpAMPClient) {
1751-
17521743
// Start a Server.
17531744
srv := internal.StartMockServer(t)
17541745
srv.EnableExpectMode()
@@ -1903,7 +1894,6 @@ func TestSendCustomMessage(t *testing.T) {
19031894
// TestCustomMessages tests the custom messages functionality.
19041895
func TestCustomMessages(t *testing.T) {
19051896
testClients(t, func(t *testing.T, client OpAMPClient) {
1906-
19071897
// Start a Server.
19081898
srv := internal.StartMockServer(t)
19091899
var rcvCustomMessage atomic.Value
@@ -2136,7 +2126,6 @@ func TestCustomMessagesSendAndWait(t *testing.T) {
21362126
// TestSetCustomCapabilities tests the ability for the client to change the set of custom capabilities that it supports.
21372127
func TestSetCustomCapabilities(t *testing.T) {
21382128
testClients(t, func(t *testing.T, client OpAMPClient) {
2139-
21402129
// Start a Server.
21412130
srv := internal.StartMockServer(t)
21422131
var rcvCustomCapabilities atomic.Value
@@ -2219,7 +2208,6 @@ func TestSetCustomCapabilities(t *testing.T) {
22192208
// TestSetFlags tests the ability for the client to change the set of flags it sends.
22202209
func TestSetFlags(t *testing.T) {
22212210
testClients(t, func(t *testing.T, client OpAMPClient) {
2222-
22232211
// Start a Server.
22242212
srv := internal.StartMockServer(t)
22252213
var rcvCustomFlags atomic.Value
@@ -2372,7 +2360,6 @@ func TestSetAvailableComponents(t *testing.T) {
23722360
for _, tc := range testCases {
23732361
t.Run(tc.desc, func(t *testing.T) {
23742362
testClients(t, func(t *testing.T, client OpAMPClient) {
2375-
23762363
// Start a Server.
23772364
srv := internal.StartMockServer(t)
23782365
srv.EnableExpectMode()

client/internal/httpsender.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,15 @@ import (
2121
"github.com/open-telemetry/opamp-go/protobufs"
2222
)
2323

24-
const OpAMPPlainHTTPMethod = "POST"
25-
const defaultPollingIntervalMs = 30 * 1000 // default interval is 30 seconds.
24+
const (
25+
OpAMPPlainHTTPMethod = "POST"
26+
defaultPollingIntervalMs = 30 * 1000 // default interval is 30 seconds.
27+
)
2628

27-
const headerContentEncoding = "Content-Encoding"
28-
const encodingTypeGZip = "gzip"
29+
const (
30+
headerContentEncoding = "Content-Encoding"
31+
encodingTypeGZip = "gzip"
32+
)
2933

3034
type requestWrapper struct {
3135
*http.Request

client/internal/httpsender_test.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
)
2222

2323
func TestHTTPSenderRetryForStatusTooManyRequests(t *testing.T) {
24-
2524
var connectionAttempts int64
2625
srv := StartMockServer(t)
2726
srv.OnRequest = func(w http.ResponseWriter, r *http.Request) {
@@ -98,7 +97,6 @@ func TestAddTLSConfig(t *testing.T) {
9897
}
9998

10099
func GenerateCertificate() (tls.Certificate, error) {
101-
102100
certPem := []byte(`-----BEGIN CERTIFICATE-----
103101
MIIBhTCCASugAwIBAgIQIRi6zePL6mKjOipn+dNuaTAKBggqhkjOPQQDAjASMRAw
104102
DgYDVQQKEwdBY21lIENvMB4XDTE3MTAyMDE5NDMwNloXDTE4MTAyMDE5NDMwNlow
@@ -126,7 +124,6 @@ EKTcWGekdmdDPsHloRNtsiCa697B2O9IFA==
126124
}
127125

128126
func TestHTTPSenderRetryForFailedRequests(t *testing.T) {
129-
130127
srv, m := newMockServer(t)
131128
address := testhelpers.GetAvailableLocalAddress()
132129
var connectionAttempts int64

client/internal/mockserver.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,10 @@ type MockServer struct {
3434
enableCompression bool
3535
}
3636

37-
const headerContentType = "Content-Type"
38-
const contentTypeProtobuf = "application/x-protobuf"
37+
const (
38+
headerContentType = "Content-Type"
39+
contentTypeProtobuf = "application/x-protobuf"
40+
)
3941

4042
func newMockServer(t *testing.T) (*MockServer, *http.ServeMux) {
4143
srv := &MockServer{
@@ -136,7 +138,7 @@ func (m *MockServer) EnableCompression() {
136138
}
137139

138140
func (m *MockServer) handleWebSocket(t *testing.T, w http.ResponseWriter, r *http.Request) {
139-
var upgrader = websocket.Upgrader{
141+
upgrader := websocket.Upgrader{
140142
EnableCompression: m.enableCompression,
141143
}
142144

client/internal/packagessyncer.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ func NewPackagesSyncer(
4747

4848
// Sync performs the package syncing process.
4949
func (s *packagesSyncer) Sync(ctx context.Context) error {
50-
5150
defer func() {
5251
close(s.doneCh)
5352
}()
@@ -162,7 +161,6 @@ func (s *packagesSyncer) syncPackage(
162161
pkgName string,
163162
pkgAvail *protobufs.PackageAvailable,
164163
) error {
165-
166164
status := s.statuses.Packages[pkgName]
167165
if status == nil {
168166
// This package has no status. Create one.
@@ -253,7 +251,8 @@ func (s *packagesSyncer) syncPackageFile(
253251
// shouldDownloadFile returns true if the file should be downloaded.
254252
func (s *packagesSyncer) shouldDownloadFile(ctx context.Context,
255253
packageName string,
256-
file *protobufs.DownloadableFile) (bool, error) {
254+
file *protobufs.DownloadableFile,
255+
) (bool, error) {
257256
fileContentHash, err := s.localState.FileContentHash(packageName)
258257

259258
if err != nil {

client/internal/wsreceiver_test.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ const (
4141
)
4242

4343
func TestServerToAgentCommand(t *testing.T) {
44-
4544
tests := []struct {
4645
command *protobufs.ServerToAgentCommand
4746
action commandAction
@@ -188,7 +187,6 @@ func TestDecodeMessage(t *testing.T) {
188187
}
189188

190189
func TestReceiverLoopStop(t *testing.T) {
191-
192190
srv := StartMockServer(t)
193191

194192
conn, _, err := websocket.DefaultDialer.DialContext(

client/wsclient_test.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,11 +194,9 @@ func TestDisconnectWSByServer(t *testing.T) {
194194
}
195195

196196
func TestVerifyWSCompress(t *testing.T) {
197-
198197
tests := []bool{false, true}
199198
for _, withCompression := range tests {
200199
t.Run(fmt.Sprintf("%v", withCompression), func(t *testing.T) {
201-
202200
// Start a Server.
203201
srv := internal.StartMockServer(t)
204202
srv.EnableExpectMode()

0 commit comments

Comments
 (0)