-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathscope_model_spec.rb
121 lines (108 loc) · 4.23 KB
/
scope_model_spec.rb
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
# encoding: ascii-8bit
# Copyright 2022 Ball Aerospace & Technologies Corp.
# All Rights Reserved.
#
# This program is free software; you can modify and/or redistribute it
# under the terms of the GNU Affero General Public License
# as published by the Free Software Foundation; version 3 with
# attribution addendums as found in the LICENSE.txt
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
# Modified by OpenC3, Inc.
# All changes Copyright 2022, OpenC3, Inc.
# All Rights Reserved
#
# This file may also be used under the terms of a commercial license
# if purchased from OpenC3, Inc.
require 'spec_helper'
require 'openc3/models/scope_model'
module OpenC3
describe ScopeModel do
before(:each) do
mock_redis()
end
describe "self.get" do
it "returns the specified scope" do
default_time = Time.now.to_nsec_from_epoch
model = ScopeModel.new(name: "DEFAULT")
model.create
sleep 0.1
other_time = Time.now.to_nsec_from_epoch
model = ScopeModel.new(name: "OTHER")
model.create
target = ScopeModel.get(name: "DEFAULT")
expect(target["name"]).to eql "DEFAULT"
expect(target["updated_at"]).to be_within(10_000_000).of(default_time)
target = ScopeModel.get(name: "OTHER")
expect(target["name"]).to eql "OTHER"
expect(target["updated_at"]).to be_within(10_000_000).of(other_time)
end
end
describe "self.names" do
it "returns all scope names" do
model = ScopeModel.new(name: "DEFAULT")
model.create
model = ScopeModel.new(name: "OTHER")
model.create
names = ScopeModel.names()
# contain_exactly doesn't care about ordering and neither do we
expect(names).to contain_exactly("DEFAULT", "OTHER")
end
end
describe "self.all" do
it "returns all the parsed scopes" do
model = ScopeModel.new(name: "DEFAULT")
model.create
model = ScopeModel.new(name: "OTHER")
model.create
all = ScopeModel.all()
expect(all.keys).to contain_exactly("DEFAULT", "OTHER")
end
end
describe "as_json" do
it "encodes all the input parameters" do
model = ScopeModel.new(name: "DEFAULT", updated_at: 12345)
json = model.as_json(:allow_nan => true)
expect(json['name']).to eql "DEFAULT"
expect(json['updated_at']).to eql 12345
end
end
describe "deploy" do
it "deploys the microservices" do
dir = File.join(SPEC_DIR, "install")
model = ScopeModel.new(name: "DEFAULT", updated_at: 12345)
model.create
model.deploy(dir, {})
# Ensure scope_model creates the UNKNOWN target and streams
target = TargetModel.get(name: "UNKNOWN", scope: "DEFAULT")
expect(target["name"]).to eql "UNKNOWN"
expect(Store.exists("DEFAULT__COMMAND__{UNKNOWN}__UNKNOWN")).to eql 1
expect(Store.exists("DEFAULT__TELEMETRY__{UNKNOWN}__UNKNOWN")).to eql 1
end
end
describe "destroy" do
it "destroys the scope and all microservices" do
s3 = instance_double("Aws::S3::Client")
allow(Aws::S3::Client).to receive(:new).and_return(s3)
options = OpenStruct.new
options.key = "blah"
objs = double("Object", :contents => [options], is_truncated: false)
scope = "TEST"
allow(s3).to receive(:list_objects_v2).and_return(objs)
allow(s3).to receive(:delete_object).with(bucket: 'config', key: "blah")
dir = File.join(SPEC_DIR, "install")
model = ScopeModel.new(name: scope, updated_at: 12345)
model.create
model.deploy(dir, {})
topics = EphemeralStore.scan_each(match: "#{scope}*", type: 'stream', count: 100).to_a.uniq.sort
expect(topics).to eql(%w(TEST__COMMAND__{UNKNOWN}__UNKNOWN TEST__CONFIG TEST__TELEMETRY__{UNKNOWN}__UNKNOWN TEST__openc3_targets))
model.destroy
topics = EphemeralStore.scan_each(match: "#{scope}*", type: 'stream', count: 100).to_a.uniq.sort
expect(topics).to eql([])
end
end
end
end