-
Notifications
You must be signed in to change notification settings - Fork 37
/
InstrumentationTest.java
148 lines (139 loc) · 5.65 KB
/
InstrumentationTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*
* Copyright 2022 Google LLC
*
* 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 com.google.cloud.logging;
import com.google.api.client.util.Lists;
import com.google.cloud.Tuple;
import com.google.cloud.logging.Payload.JsonPayload;
import com.google.cloud.logging.Payload.StringPayload;
import com.google.cloud.logging.Payload.Type;
import com.google.common.collect.ImmutableList;
import com.google.protobuf.ListValue;
import com.google.protobuf.Value;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
@RunWith(JUnit4.class)
public class InstrumentationTest {
private static final StringPayload STRING_PAYLOAD = StringPayload.of("payload");
private static final LogEntry STRING_ENTRY = LogEntry.newBuilder(STRING_PAYLOAD).build();
private static final String JAVA_OTHER_NAME = "java-other";
private static final String JAVA_INVALID_NAME = "no-java-name";
private static final String JAVA_OTHER_VERSION = "1.0.0";
@Test
public void testInstrumentationGenerated() {
Instrumentation.setInstrumentationStatus(false);
verifyEntries(
Instrumentation.populateInstrumentationInfo(ImmutableList.of(STRING_ENTRY)),
1,
2,
new HashSet<>(Arrays.asList(Instrumentation.JAVA_LIBRARY_NAME_PREFIX)),
new HashSet<>(
Arrays.asList(Instrumentation.getLibraryVersion(Instrumentation.class.getClass()))));
}
@Test
public void testNoInstrumentationGenerated() {
Instrumentation.setInstrumentationStatus(true);
Tuple<Boolean, Iterable<LogEntry>> pair =
Instrumentation.populateInstrumentationInfo(ImmutableList.of(STRING_ENTRY));
ArrayList<LogEntry> entries = Lists.newArrayList(pair.y());
Assert.assertFalse(pair.x());
Assert.assertEquals(entries.size(), 1);
Assert.assertTrue(entries.get(0).getPayload().getType() == Type.STRING);
}
@Test
public void testInstrumentationUpdated() {
Instrumentation.setInstrumentationStatus(false);
LogEntry json_entry =
LogEntry.newBuilder(generateInstrumentationPayload(JAVA_OTHER_NAME, JAVA_OTHER_VERSION))
.build();
verifyEntries(
Instrumentation.populateInstrumentationInfo(ImmutableList.of(json_entry)),
0,
1,
new HashSet<>(Arrays.asList(Instrumentation.JAVA_LIBRARY_NAME_PREFIX, JAVA_OTHER_NAME)),
new HashSet<>(
Arrays.asList(
Instrumentation.getLibraryVersion(Instrumentation.class.getClass()),
JAVA_OTHER_VERSION)));
}
@Test
public void testInvalidInstrumentationRemoved() {
Instrumentation.setInstrumentationStatus(false);
LogEntry json_entry =
LogEntry.newBuilder(generateInstrumentationPayload(JAVA_INVALID_NAME, JAVA_OTHER_VERSION))
.build();
verifyEntries(
Instrumentation.populateInstrumentationInfo(ImmutableList.of(json_entry)),
0,
1,
new HashSet<>(Arrays.asList(Instrumentation.JAVA_LIBRARY_NAME_PREFIX)),
new HashSet<>(
Arrays.asList(Instrumentation.getLibraryVersion(Instrumentation.class.getClass()))));
}
public static JsonPayload generateInstrumentationPayload(
String libraryName, String libraryVersion) {
Map<String, Object> json_data = new HashMap<>();
Map<String, Object> instrumentation_data = new HashMap<>();
Map<String, Object> info = new HashMap<>();
info.put(Instrumentation.INSTRUMENTATION_NAME_KEY, libraryName);
info.put(Instrumentation.INSTRUMENTATION_VERSION_KEY, libraryVersion);
List<Object> list = ImmutableList.<Object>of(info);
instrumentation_data.put(Instrumentation.INSTRUMENTATION_SOURCE_KEY, list);
json_data.put(Instrumentation.DIAGNOSTIC_INFO_KEY, instrumentation_data);
return JsonPayload.of(json_data);
}
private static void verifyEntries(
Tuple<Boolean, Iterable<LogEntry>> pair,
int index,
int expected,
HashSet<String> names,
HashSet<String> versions) {
ArrayList<LogEntry> entries = Lists.newArrayList(pair.y());
Assert.assertTrue(pair.x());
Assert.assertEquals(entries.size(), expected);
Assert.assertTrue(entries.get(index).getPayload().getType() == Type.JSON);
ListValue infoList =
entries
.get(index)
.<Payload.JsonPayload>getPayload()
.getData()
.getFieldsOrThrow(Instrumentation.DIAGNOSTIC_INFO_KEY)
.getStructValue()
.getFieldsOrThrow(Instrumentation.INSTRUMENTATION_SOURCE_KEY)
.getListValue();
for (Value val : infoList.getValuesList()) {
Assert.assertTrue(
names.remove(
val.getStructValue()
.getFieldsOrThrow(Instrumentation.INSTRUMENTATION_NAME_KEY)
.getStringValue()));
Assert.assertTrue(
versions.remove(
val.getStructValue()
.getFieldsOrThrow(Instrumentation.INSTRUMENTATION_VERSION_KEY)
.getStringValue()));
}
Assert.assertEquals(names.size(), 0);
Assert.assertEquals(versions.size(), 0);
}
}